1. Rationale
    1. API
      1. use-callables
      2. callable
      3. lambda*
      4. define*
      5. dotted-lambdas
    2. Examples
  2. Requirements
  3. Last update
  4. Author
  5. License
  6. Version history

Rationale

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

Using the appropriate dots-type gives you additional control over the length of the variable argument lists.

In this version, we have introduced a parameter, use-callables, which cotrols, if the rest argument of the argument list is a normal or a callable list. Normal lists are the default, but callable lists make some uses easier. After all, (lst 3) is much easier 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.

API

use-callables

[parameter] (use-callables)
[parameter] (use-callables ok?)

when set, the rest argument of the argument list is a callable list. Otherwise (the default) it's an ordinary list.

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, with one argument, an index, it returns the list's value at that index, and with a negative index, it returns the list's length.

lambda*

[syntax] (lambda* (x ... xs ..) xpr . xprs)
[syntax] (lambda* (x ... xs ...) xpr . xprs)
[syntax] (lambda* (x ... xs ....) xpr . xprs)
[syntax] (lambda* (x ...) xpr . xprs)

the first three evaluate to (lambda (x ... . xs) xpr . xprs) making xs callable provided (use-callables) is set, 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] (define* (name x ... xs ..) xpr . xprs)
[syntax] (define* (name x ... xs ...) xpr . xprs)
[syntax] (define* (name x ... xs ....) xpr . xprs)
[syntax] (define* (name x ...) xpr . xprs)

syntactic sugar.

dotted-lambdas

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

with sym: documentation of exported symbol without sym: list of exported symbols

Examples


(import dotted-lambdas)

(verbose? #t)

(condition-case (lst0 0) ((exn) #f))
;-> #f

(lst 0)
;-> 0

(lst 3)
;-> 3

(condition-case (lst 5) ((exn) #f))
;-> #f

(lst -1)
;-> 4

(use-callables #f)
;-> #f

((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 ())

(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 ())

(use-callables #t)
;-> #t

((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))

((lambda* (xs ...) (xs)))
;-> (quote ())

((lambda* (x y zs ...) (list x y (zs 0) (zs 1))) 1 2 3 4)
;-> (quote (1 2 3 4))

((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 ())

(define*
  (inc x i ..)
  (if (use-callables) (+ x (optional (i) 1)) (+ x (optional i 1))))

(define* (foo x y zs ...) (if (use-callables) (list x y (zs)) (list x y zs)))

(define* (bar x ys ....) (if (use-callables) (list x (ys)) (list x ys)))

(define* (baz xs ..) (if (use-callables) (list (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

Requirements

None

Last update

Nov 17, 2022

Author

Juergen Lorenz

License

Copyright (c) 2022 , 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.

Version history

1.3.1
tests updated
1.3
parameter use-callables added
1.2
new test version
1.1
define* added
1.0
initial check in