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, which are ledal identifiers, is
- two dots: the argument to the left references a callable list with zero or one items
- three dots: the argument to the left references a callable list
- four dots: the argument to the left references a nonempty callable list
Using the appropriate dots-type gives you additional control over the length of the variable argument lists.
We've used callable lists instead of ordinary lists for ease of use. Indeed, (lst 3) is much easyer on the fingers than (list-ref lst 3).
Note, that there is an egg written by Mario, callable-data-structures, and one written by me, callable-sequences, which could have been used instead. But since we only need list arguments and their values on indexes, the only necessary routine, callable, is defined here, so that there are no dependencies.
The implementation of the macro is quite simple. I use a not so well-known Chicken extension of syntax-rules, which allows to replace the ellipsis with another symbol, here !!!. So I can use .., ... and .... as keywords.
- callable lstprocedure
makes the list argument, lst, callable, i.e. encapsulates it in a procedure of zero or one argument. With no argument it returns the encapsulated list and with one argument, an index, returns the list's value at that index.
- (lambda* (x ...) xpr . xprs)syntax
the first three evaluate to (lambda (x ... . xs) xpr . xprs) making xs callable and checking if xs is of length at most 1, arbitrary or at least1, respectively. The last one evaluates to ordinary (lambda (x ...) xpr . xprs)
- dotted-lambdas symprocedure
with sym: documentation of exported symbol without sym: list of exported symbols
(import dotted-lambdas) (condition-case (lst0 0) ((exn) #f)) ;-> #f (lst 0) ;-> 0 (lst 3) ;-> 3 (condition-case (lst 5) ((exn) #f)) ;-> #f (condition-case (lst -1) ((exn) #f)) ;-> #f ((lambda* (xs ...) (list (xs 0) (xs 1) (xs 2))) 1 2 3) ;-> (quote (1 2 3)) ((lambda* (x xs ...) (apply list x (xs))) 1 2 3) ;-> (quote (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))) ;-> (quote ()) ((lambda* (x y zs ...) (list x y (zs 0) (zs 1))) 1 2 3 4) ;-> (quote (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) ;-> (quote (1 2)) ((lambda* (as ...) (as)) 1 2 3) ;-> (quote (1 2 3)) ((lambda* (as ...) (as))) ;-> (quote ())
Sep 4, 2020
Copyright (c) 2020 , Juergen Lorenz, ju (at) jugilo (dot) de 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.
- Initial check in