How to set default or optional parameters in scheme?

Viewed 3117

I'm trying to figure out how to how to set default or optional parameters in Scheme.

I've tried (define (func a #!optional b) (+ a b)) but I can't find of a way to check if b is a default parameter, because simply calling (func 1 2) will give the error:

Error: +: number required, but got #("halt") [func, +]

I've also tried (define (func a [b 0]) (+ a b)) but I get the following error:

Error: execute: unbound symbol: "b" [func]

If it helps I'm using BiwaScheme as used in repl.it

3 Answers

MIT/GNU Scheme doc for this purpose.

(define (f a #!optional b)
  (+ a
     (if (default-object? b)
         0
         b)))

; test
(f 1) ; 1
(f 1 2) ; 3
Related