You are looking at historical revision 38830 of this page. It may differ significantly from its current revision.

Lambdas with dots as symbols

Scheme lambdas allow for variable length argument lists by using a dotted list as argument list.

dotted-lambdas use ordinary lists of length at least 2 instead, whose last item is one of the symbols .., ... or ....

The meaning of theses symbols is

We've used callable instead of ordinary lists for ease of use. Indeed, (lst 3) is much easyer on the fingers than (list-ref lst 3). Moreover, they are much more flexible, because they allow for slices, e.g. (lst 2 4) and (lst 4 2).

Note, that there is an egg written by Mario, named callable-data-structures, which could have been used instead.

The implementation of the macro is quite simple. I use a Chicken extension of syntax-rules, which allows to replace ellipses with another symbol, here !!!. So I can use e.g. ... as a keyword.

API

dotted-lambdas

[procedure] (dotted-lambdas)
[procedure] (dotted-lambdas sym)

returns the list of exported symbols, if called without argument, or the documentation of sym otherwise

lambda*

[syntax] (lambda* args xpr . xprs)

a version of lambda, where args is either a lambda-list as in ordinary lambda, or a list of at least two arguments, where the last one is one of the symbols .., ... or .... and the second to last references a callable list of at most one, arbitrary many or at least one items respectively in the lambda*'s body xpr . xprs

Requirements

callables

Examples


(import dotted-lambdas callables (chicken condition))

((lambda* (xs ...)
   (list (xs 0) (xs 1) (xs 2))) 1 2 3)
; -> '(1 2 3)
(condition-case
  ((lambda* (xs ..) xs) 1 2 3)
  ((exn) #f))
; -> #f
(condition-case
  ((lambda* (xs ....) xs))
  ((exn) #f))
; -> #f
((lambda* (xs ...) (xs)))
; -> '()
((lambda* (x y zs ...) (list x y (zs 0) (zs 1))) 1 2 3 4)
; -> '(1 2 3 4)
(condition-case
  ((lambda* (x y zs ..) (list x y zs)) 1 2 3 4)
  ((exn) #f))
; -> #f
(condition-case
  ((lambda* (x y zs ....) (list x y zs)) 1 2)
  ((exn) #f))
; -> #f
((lambda* (a b) (list a b)) 1 2)
; -> '(1 2)
((lambda* (a b . cs) (list a b cs)) 1 2 3 4)
; -> '(1 2 (3 4))
((lambda* as as) 1 2 3)
; -> '(1 2 3)
((lambda* as as))
; -> '()

Last update

Aug 11, 2020

Author

Juergen Lorenz

License

Copyright (c) 2020, Juergen Lorenz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission. 
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version History

1.0 ; initial version