DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(guile.info.gz) SRFI-16

Info Catalog (guile.info.gz) SRFI-14 (guile.info.gz) SRFI Support (guile.info.gz) SRFI-17
 
 39.13 SRFI-16 - case-lambda
 ===========================
 
 The syntactic form `case-lambda' creates procedures, just like
 `lambda', but has syntactic extensions for writing procedures of
 varying arity easier.
 
    The syntax of the `case-lambda' form is defined in the following
 EBNF grammar.
 
      <case-lambda>
         --> (case-lambda <case-lambda-clause>)
      <case-lambda-clause>
         --> (<formals> <definition-or-command>*)
      <formals>
         --> (<identifier>*)
           | (<identifier>* . <identifier>)
           | <identifier>
 
    The value returned by a `case-lambda' form is a procedure which
 matches the number of actual arguments against the formals in the
 various clauses, in order.  "Formals" means a formal argument list just
 like with `lambda' ( Lambda). The first matching clause is
 selected, the corresponding values from the actual parameter list are
 bound to the variable names in the clauses and the body of the clause is
 evaluated.  If no clause matches, an error is signalled.
 
    The following (silly) definition creates a procedure FOO which acts
 differently, depending on the number of actual arguments.  If one
 argument is given, the constant `#t' is returned, two arguments are
 added and if more arguments are passed, their product is calculated.
 
      (define foo (case-lambda
                    ((x) #t)
                    ((x y) (+ x y))
                    (z
                      (apply * z))))
      (foo 'bar)
      =>
      #t
      (foo 2 4)
      =>
      6
      (foo 3 3 3)
      =>
      27
      (foo)
      =>
      1
 
    The last expression evaluates to 1 because the last clause is
 matched, Z is bound to the empty list and the following multiplication,
 applied to zero arguments, yields 1.
 
Info Catalog (guile.info.gz) SRFI-14 (guile.info.gz) SRFI Support (guile.info.gz) SRFI-17
automatically generated byinfo2html