2
(lambda (head . rest)
     (...))

I encountered this code on the net when trying to learn some scheme, but I couldn't find any useful explanation.

What is the meaning of this? Is it some kind of pattern matching as in ML? (Btw they apply this lambda to only one argument!)

4

2 回答 2

3

In this procedure:

(lambda (arg . args)
     (...))

The syntax indicates that the lambda form is expecting one mandatory argument bound to the name arg and a list with zero or more elements (a variable number of arguments) bound to the name args. This is an example of a variadic function.

The same syntax can be used for named procedures, noticing that any number of parameters can be specified as mandatory and after that the rest are considered optional; take a look at this example:

(define (test arg . args)
  (apply + arg args))

(test)        ; will cause an error, at least one argument is expected
(test 10)     ; returns 10
(test 10 4)   ; returns 14
(test 10 4 2) ; returns 16
于 2012-11-19T19:04:59.027 回答
2

This is »dotted-tail notation«. It allows you to pass an abritrary number of arguments to a function. For an explanation consider SICP, exercise 2.20 (please scroll down).

于 2012-11-19T18:36:50.460 回答