More‎ > ‎

Smart argument lists

Arc 3.1 supports several features in argument lists of functions and macros. You can specify optional arguments:

> (def foo (a (o b)) (cons a b))
> (foo 3)
(3)  ; b defaults to nil
> (def foo (a (o b 4)) (cons a b))
> (foo 3)
(3 . 4)

You can collect a variable number of arguments into a rest arg.

> (def foo (a . b) b)     ; b is a rest arg
> (foo 3)
nil                       ; rest args are optional
> (foo 3 4)
(4)
> (foo 3 4 5)
(4 5)                     ; b is a list of remaining args after a

You can also specify a complex structure that you will provide arguments in, and arc will destructure it to get at the arguments.

> (def foo(a (b c)) (+ a b c)
> (foo 3 (4 5))
12
> (def foo(a (b . c)) (+ a b (car c)))
> (foo 3 (4))
7
> (foo 3 (4 5 6))
12


http://github.com/akkartik/arc implements an additional feature: optional keyword arguments. These will look familiar if you know python.
> (def foo (a b) (cons a b))

> (foo 3 4)
(3 . 4) ; expected

> (foo :b 3 4)
(4 . 3) ; whoa!
http://github.com/akkartik/arc also experiments with a different syntax for optional args:

> (def foo (a ? b 4) (cons a b))
> (foo 3)
(3 . 4)

; you must provide defaults for all optional args
> (def foo (a ? b nil c 4) (list a b c))
> (foo 3)
(3 nil 4)

See the unit tests for more examples.
Comments