I'm learning Guile Scheme at the moment, and in the documentation I suddenly ran into the following construction:
((lambda args (display args)) 42)
=> (42)
This threw me for a loop; up until this point I had assumed formal parameters were always enclosed in a list:
((lambda (args) (display args)) 42)
=> 42
I wonder when to use this variant, and how this differs from the dot notation for variable number of arguments. Specifically, what is the difference between the two following variants:
((lambda args (display args)) 1 2 3) => (1 2 3)
((lambda (. args) (display args)) 1 2 3) => (1 2 3)
Is there a difference — perhaps for more complex examples — that I need to be aware of and is there any reason to prefer one over the other?
The difference is that this version receives a single parameter called
args
, useful for the cases when you know exactly the number of actual arguments expected for thelambda
form:And this version receives a (possibly empty) list of parameters, called
args
, useful when you expect a variable number of arguments for thelambda
form:There should be no difference between the following two versions, but not all interpreters will accept the second one, since it's missing the part before the dot (therefore it should be avoided):
The following version is useful when you want to specify that a
lambda
form has one or more mandatory parameters (symbols to the left of the dot) and a list of zero or more optional parameters (a single symbol to the right of the dot):