previous up next     contents index
Next: Arity Up: Procedures Previous: Procedures

Dispatching

The  case-lambda form creates a procedure that dispatches to a body of expressions based on the number of arguments it receives.

A case-lambda expression has the form:

 
   (case-lambda
     (formals expr  tex2html_wrap_inline8724 )
      tex2html_wrap_inline8724 ) 
Each clause of a case-lambda expression is analogous to a closure of the form (lambda formals expr tex2html_wrap_inline8724 ). A case-lambda expression can have any number of clauses and the order of the clauses is important: when a case-lambda procedure is applied to n arguments, it evaluates the body of the first clause that can accept n values for its formals parameter list.

For example:

 
   (define f
      (case-lambda
         [(x) x]
         [(x y) (+ x y)]
         [(a . any) a]))
   (f 1) ; => 1
   (f 1 2) ; => 3
   (f 4 5 6 7) ; => 4
   (f) ; raises exn:application:arity 

The  procedure? predicate returns #t when applied to the result of a case-lambda expression.



PLT