Previous: , Up: srfi case-lambda   [Index]


2.10.4 Specification

Syntax: case-lambda clause ...

Eeach clause has the format (formals body), were formals is a formal arguments list as for lambda (cf section 4.1.4 of the R5RS). Each body is a tail-body (cf section 3.5 of the R5RS).

A case-lambda expression evaluates to a procedure that accepts a variable number of arguments and is lexically scoped in the same manner as procedures resulting from lambda expressions.

When the procedure is called with some arguments V1, ..., Vk, then the first clause for which the arguments agree with formals is selected, where agreement is specified as for the formals of a LAMBDA expression.

The variables of formals are bound to fresh locations, the values V1, ..., Vk are stored in those locations, the body is evaluated in the extended environment, and the results of body are returned as the results of the procedure call.

It is an error for the arguments not to agree with the formals of any clause.

Error:

(define plus
  (case-lambda
    [()         0]
    [(x)        x]
    [(x y)      (+ x y)]
    [(x y z)    (+ (+ x y) z)]
    [args       (apply + args)]))

(plus)                     => 0
(plus 1)                   => 1
(plus 1 2 3)               => 6

((case-lambda
   [(a)         a]
   [(a b)       (* a b)])
 1 2 3)                    => error