tur*_*ete 2 list common-lisp higher-order-functions
我有一个函数列表,一个元素列表,我想在所有元素上应用所有函数,然后将所有结果列表附加在一起.我按照以下方式做到了
(defun apply-functions(funcs elements)
(if (null funcs)
nil
(append (mapcar #'(lambda (x) (funcall (car funcs) x)) elements) (apply-functions (rest funcs) elements))))
Run Code Online (Sandbox Code Playgroud)
它按预期工作,但我不喜欢它.这样做有更干净,更简洁的方法吗?我是lisp的新手,并且仍然习惯于lispish做事.
我不知道你是否喜欢loop宏(我不想破坏任何人),但试试这个:
(defun apply-functions (fs es)
(loop for f in fs appending (mapcar f es)))
Run Code Online (Sandbox Code Playgroud)
小智 5
这与你的想法相同,只是更短:
(defun apply-functions (functions elements)
(mapcan #'(lambda (x) (mapcar x elements)) functions))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
406 次 |
| 最近记录: |