How to accept uncertain amount of arguments is by using rest argument. ALmost all languages has this and in Scheme you use a variable instead of list with a variable in it. eg.
(define my-list
(lambda arguments
arguments))
(my-list 1 2 3 4)
; ==> (1 2 3 4)
Also the shorthand define
for functions which is the same:
(define (my-list . arguments)
arguments)
Imagine that you want a function that takes 2 arguments and in addition an unknown amount of arguments after that:
(define (test a b . rest)
(list a b rest))
(test 1 2 3 4 5)
; ==> (1 2 (3 4 5))
Also the opposite mirror operation is perhaps interesting, which is apply
. Imagine you want (+10 1 2 3) ; ==> 16
and that it should take any number of arguments. It can be done like this:
(define (+10 . args)
(apply + 10 args))
Apply takes a function, then the second until the next to last arguments (may be none) as the first arguments to that function and then the last argument, which is a zero or more element proper list, as the last arguments to the function. Thus:
(apply + 1 2 3 '(4 5 6)) ; ==
(+ 1 2 3 4 5 6)
A common pearl of a function made with this is zip
:
(define (zip . args)
(apply map list args))
(zip '(1 2 3 4)
'(a b c d)
'(I II III IV))
; ==> ((1 a I)
; (2 b II)
; (3 c III)
; (4 d IV))
And you have unzip
, which does the same as zip
except it takes a list of lists as one argument. Passing the result through unzip
twice recreates the one agrument:
(define (unzip lsts)
(apply zip lsts))
(unzip '((1 a) (2 b)))
; ==> ((1 2) (a b))
(unzip (unzip '((1 a) (2 b))))
; ==> ((1 a) (2 b))