s c h e m a t i c s : c o o k b o o k

/ Cookbook.IdiomOptionalArguments

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Optional arguments

Problem

Some functions take arguments that only in exceptional cases are different from a default value. Optional arguments makes it possible only to give the extra arguements in the exceptional case.

Solution

Two different solutions are presented below.

The most direct us using opt-lambda. The opt-lambda form behaves like a normal lambda form, except that a default value can be attached to a parameter by replacing parameter with [parameter default-value-expression]. Note that alle variables after an optional arguemtn must also have default value expression.

The second solution uses case-lambda to catch the cases where the optional arguments are missing. They are then provided in a recursive call.

The example features the function count-down. In most cases a count down to 0 is wanted, but the end point can be given.

;; (count-down n m) = (list n n-1 n-2 ... m)
;; (count-down n)   = (list n n-1 ... 0)

; (interval s t) = (t t+1 ... t+m-1)
(define (interval s t)
  (if (> s t)
    '()
    (cons s (interval (+ s 1) t)))


(define count-down
  (opt-lambda (n [m 0])
    (reverse (interval (+ (- n m) 1) m))))

(count-down 10)    ; => (10 9 8 7 6 5 4 3 2 1 0)
(count-down 10 4)  ; => (10 9 8 7 6 5 4)

(define count-down
  (case-lambda
    [(n)   (count-down n 0)]
    [(n m) (reverse (interval (+ (- n m) 1) m))]))

(count-down 10)    ; => (10 9 8 7 6 5 4 3 2 1 0)
(count-down 10 4)  ; => (10 9 8 7 6 5 4)

Discussion

The case-lambda construct is a primitive in PLT Scheme. In other Schemes remember to load SRFI 16 (Syntax for procedures of variable arity. )

The opt-lambda macro is implemented in terms of case-lambda.

Contributors

-- JensAxelSoegaard -- NoelWelsh

Comments

I think it would be best not to require SRFI-1 to use "iota" in an example. The use looked complicated, and is not standard Scheme, when the intent is to illustrate optional arguments. Below is an R5RS count-down, but perhaps simpler example like incremented would be best (if that's not too simple):

(require (lib "etc.ss"))

(define count-down
  (opt-lambda (start (end 0))
    (let loop ((i start))
      (if (< i end)
          '()
          (cons i (loop (- i 1)))))))

(require (lib "etc.ss"))

(define incremented
  (opt-lambda (number (increment 1))
    (+ number increment)))

(incremented 42)    ; => 43
(incremented 42 27) ; => 69

Also we should note that opt-lambda requires (lib "etc.ss").

-- NeilVanDyke - 17 May 2004

I renamed iota to interval and defined without using SRFI9.

-- JensAxelSoegaard - 21 May 2004

CookbookForm
TopicType: Recipe
ParentTopic: CommonIdiomsRecipes?
TopicOrder:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest