; 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. #|[ 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. ]|# (module dotted-lambdas ( callable lambda* dotted-lambdas ) (import scheme (only (chicken base) print error case-lambda) ) #|[ (callable lst) --- procedure --- 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. ]|# (define (callable lst) (let ((len (length lst))) (case-lambda (() lst) ((k) (list-ref lst k))))) #|[ (lambda* (x ... xs ..) xpr . xprs) (lambda* (x ... xs ...) xpr . xprs) (lambda* (x ... xs ....) xpr . xprs) (lambda* (x ...) xpr . xprs) --- macro --- 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) ]|# (define-syntax lambda* (syntax-rules !!! (.. ... ....) ((_ (x !!! xs ..) xpr . xprs) (lambda (x !!! . xs) (if (or (null? xs) (null? (cdr xs))) (let ((xs (callable xs))) xpr . xprs) (error 'lambda* "too many arguments for .." xs)))) ((_ (x !!! xs ...) xpr . xprs) (lambda (x !!! . xs) (let ((xs (callable xs))) xpr . xprs))) ((_ (x !!! xs ....) xpr . xprs) (lambda (x !!! . xs) (if (null? xs) (error 'lambda* "not enough arguments for ...." xs) (let ((xs (make-callable xs))) xpr . xprs)))) ((_ (x !!!) xpr . xprs) ; without dots: normal lambda (lambda (x !!!) xpr . xprs)) )) ; Note, that ... as well as .. and .... are legal Scheme identifiers #|[ (dotted-lambdas) (dotted-lambdas sym) --- procedure --- documentation procedure ]|# (define dotted-lambdas (let ( (alist '( (callable procedure: (callable lst) "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* macro: (lambda* (x ... xs ..) xpr . xprs) (lambda* (x ... xs ...) xpr . xprs) (lambda* (x ... xs ....) xpr . xprs) (lambda* (x ...) xpr . xprs) "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 procedure: (dotted-lambdas) (dotted-lambdas sym) "with sym: documentation of exported symbol" "without sym: list of exported symbols" ) )) ) (case-lambda (() (map car alist)) ((sym) (let ((pair (assq sym alist))) (if pair (for-each print (cdr pair)) (print "Choose one of " (map car alist)))))))) )