I want to define a function in Racket with undefined number of arguments so I use ellipsis, but it doesn't work:
(define (f x ...) (printf x ...))
(f "~a ~a" "foo" "bar")
Error:
Arity mismatch
How to define a function in Racket with ellipsis?
There are two halves of this:
To accept an arbitrary number of inputs, instead of a
...afterx, put a single.beforex. This declaresxas the "rest" argument, and the arguments will be collected into a list forx.Example:
To pass an arbitrary number of arguments, you can use the
applyfunction, which accepts a list as the last argument.Example:
Putting these together: