www.delorie.com/gnu/docs/guile/guile_64.html   search  
Buy GNU books!

Guile Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.2.4 Lambda Alternatives

Since it is so common in Scheme programs to want to create a procedure and then store it in a variable, there is an alternative form of the define syntax that allows you to do just that.

A define expression of the form

(define (name [arg1 [arg2 ...]])
  expression ...)

is exactly equivalent to the longer form

(define name
  (lambda ([arg1 [arg2 ...]])
    expression ...))

So, for example, the definition of make-combined-string in the previous subsection could equally be written:

(define (make-combined-string name address)
  (string-append "Name=" name ":Address=" address))

This kind of procedure definition creates a procedure that requires exactly the expected number of arguments. There are two further forms of the lambda expression, which create a procedure that can accept a variable number of arguments:

(lambda (arg1 ... . args) expression ...)

(lambda args expression ...)

The corresponding forms of the alternative define syntax are:

(define (name arg1 ... . args) expression ...)

(define (name . args) expression ...)

For details on how these forms work, see See section 23.1 Lambda: Basic Procedure Creation.

(It could be argued that the alternative define forms are rather confusing, especially for newcomers to the Scheme language, as they hide both the role of lambda and the fact that procedures are values that are stored in variables in the some way as any other kind of value. On the other hand, they are very convenient, and they are also a good example of another of Scheme's powerful features: the ability to specify arbitrary syntactic transformations at run time, which can be applied to subsequently read input.)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003