以编程方式在函数中嵌入多个List元素

时间:2013-09-11 00:11:36

标签: lisp common-lisp

我正在搞乱网络开发,并希望做类似以下的事情:

(defun col1 (&rest content) `((:DIV :CLASS "col1") 
                              (:COMMENT " Column 1 start ")
                              ,content goes here
                              (:COMMENT " Column 1 end ")))

其中content是函数返回的两个或多个列表:

(defun two-list () ....)

将返回

'(:H2 "header")
'(:P "paragraph")

这样

(col1 (two-list))

将返回

((:DIV :CLASS "col1") 
 (:COMMENT " Column 1 start ")
 (:H2 "header")
 (:P "paragraph")
 (:COMMENT " Column 1 end "))    

我尝试过使用values函数,但似乎只将一个列表嵌入到内容区域中。可以这样做吗?感谢帮助lispers,我正在学习如何使用Lisp!

1 个答案:

答案 0 :(得分:4)

两个列表如何返回要嵌入的元素列表,例如((:H2 "header")(:P "paragraph")),然后col1可以定义为:

(defun col1 (content) `((:DIV :CLASS "col1") 
                              (:COMMENT " Column 1 start ")
                              ,@content ; goes here
                              (:COMMENT " Column 1 end ")))

(defun two-list () '((:H2 "header")(:P "paragraph")))

请注意,我从col1中删除了&rest。如果您还想要,则需要(apply #'col1 (two-list))代替(col1 (two-list))