Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spread a list into parent sexp

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

(+ (spread '(1 2 3))) -> (+ 1 2 3)
like image 950
Jorge Martinez Avatar asked Jan 16 '23 19:01

Jorge Martinez


1 Answers

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
like image 136
dkim Avatar answered Jan 22 '23 21:01

dkim