4

Is there a form in any lisp that could "spread" a list in the parent sexp? Like:

(+ (spread '(1 2 3))) -> (+ 1 2 3)

1 Answers1

9

There are two way to do it. Which is better depends on what you want in the end.

Generally, you can use ,@ inside ` (backquote). The form following ,@ is evaluated to produce a list, which is then spliced into the template:

* `(+ ,@'(1 2 3))
(+ 1 2 3)

* (eval `(+ ,@'(1 2 3)))
6

Or, if you just want to call a function with its arguments which are packed in a list, it will be more convenient to use the apply function:

* (apply #'+ '(1 2 3))
6
dkim
  • 3,930
  • 1
  • 33
  • 37
  • oh! i kept seeing `,@` in source files and examples, but i never did figure out what it did (until now). thank you! – Jorge Martinez Aug 18 '12 at 05:11
  • Yeah, if you're just trying to have this passed in to a function like `+`, then the `apply` part of this answer is very likely what's wanted. – lindes Aug 18 '12 at 20:16
  • very good ! i second the apply approach though cause it's schemer. ;) – ramrunner Aug 18 '12 at 21:17