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 legal 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. Returns the list's length with negative index.
- (lambda* (x !!! xs ....) xpr . xprs)syntax
An alternative to (lambda (x ... . xs) xpr . xprs). Makes xs callable and checks if xs is of length at most 1, arbitrary or at least1, respectively.
- (define name (lambda* (x !!! xs ..) xpr . xprs))syntax
syntactic sugar for, e.g.
- 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 (lst -1) ;-> 4 (condition-case (lst 5) ((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* (as ...) (as)) 1 2 3) ;-> (quote (1 2 3)) ((lambda* (as ...) (as))) ;-> (quote ()) (define* (inc x i ..) (+ x (optional (i) 1))) (define* (foo x y zs ...) (list x y (zs))) (define* (bar x ys ....) (list x (ys))) (define* (baz xs ..) (list (xs))) (inc 5) ;-> 6 (inc 5 3) ;-> 8 (foo 1 2 3 4) ;-> (quote (1 2 (3 4))) (foo 1 2) ;-> (quote (1 2 ())) (bar 1 2 3 4) ;-> (quote (1 (2 3 4))) (condition-case (bar 1) ((exn) #f)) ;-> #f (baz 1) ;-> (quote ((1))) (condition-case (baz 1 2) ((exn) #f)) ;-> #f
Feb 8, 2021
This egg is hosted on the CHICKEN Subversion repository:
If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.
Copyright (c) 2021 , 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.
- new test version
- define* added
- Initial check in