Arc Forumnew | comments | leaders | submitlogin
"&rest" operator for macros/functions in Arc?
2 points by orthecreedence 5158 days ago | 4 comments
I've been looking at the forum for a bit now and also done many google searches, but can't seem to find how to have something synonymous to CL's &rest as the only argument to a macro (or function):

(defmacro lol (&rest all-args) `(list ,@all-args))

I get that (mac ...) has the dotted syntax:

(mac lol (first . rest) ...)

but this forces me to treat "first" differently from "rest" even though I want them to essentially be the part of the same list. Am I missing something really obvious?



2 points by Pauan 5158 days ago | link

Just put in a symbol, like so:

  (mac lol args ...)
Now args is a list of all the arguments.

-----

3 points by rocketnia 5158 days ago | link

What we're looking at here is the base case of a dotted list:

  (a b . rest)
  (a . rest)
  rest
The syntax for a single cons cell is (a . b). The (a b c . d) syntax is a shorthand for (a . (b . (c . d))), and the (a b c) syntax is a shorthand for (a b c . ()), where () is nil. Here's a bit of a demonstration:

  arc>
    (def inspect-conses (x)
      (treewise (fn (a b) (+ "(" a " . " b ")")) [tostring:write _] x))
  #<procedure: inspect-conses>
  arc> (inspect-conses '(a b c))
  "(a . (b . (c . nil)))"
  arc> (inspect-conses '(a b . c))
  "(a . (b . c))"
  arc> (inspect-conses '(mac lol (first second . rest) ...))
  "(mac . (lol . ((first . (second . rest)) . (... . nil))))"
  arc> (inspect-conses '(mac lol (first . rest) ...))
  "(mac . (lol . ((first . rest) . (... . nil))))"
  arc> (inspect-conses '(mac lol all-args ...))
  "(mac . (lol . (all-args . (... . nil))))"
If you come from Common Lisp, maybe you already know all this, but I hope it helps someone out there. :-p

-----

1 point by orthecreedence 5158 days ago | link

Thanks for the help, guys!

-----

3 points by akkartik 5158 days ago | link

..and the arc tutorial (http://ycombinator.com/arc/tut.txt) brings up rest args and more.

-----