Spread a list into parent sexp

故事扮演 提交于 2019-12-10 19:45:24

问题


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:


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


来源:https://stackoverflow.com/questions/12015885/spread-a-list-into-parent-sexp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!