Arc Forumnew | comments | leaders | submitlogin
4 points by tokipin 6042 days ago | link | parent

you can use the . as meaning "rest"

  (def sqnl (a b . c) ...)
this is a function that takes a mininum of 2 arguments. when called, the extra arguments are combined into a list and bound to c

for full variadic functions, you can just leave off the parens:

  (def sqnl args
      (map [* _ _] args))
a really nifty thing is that the arguments are automatically destructured (dunno if that's the term.) for example, say you are using lists of two elements to represent points on a coordinate plane:

  (= p1 (list 30 50)
     p2 (list 20 80))
and say you want to make a function that multiplies the points by a scalar. you can do it like this:

  (def scale (point s)
       (list (* s (car point))
             (* s (cadr point))))
so you're explicitly accessing the first (car) and second (cadr) parts of the list. but you can have the work done for you like so:

  (def scale ((x y) s)
       (list (* s x)
             (* s y)))