我发现自己一直在做这种事情.我一直在考虑编写一个宏/函数来使这种事情更容易,但我发现我可能正在重新发明轮子.
是否有现有的功能可以让我更简洁地完成同样的事情?
(defun remove-low-words (word-list) "Return a list with words of insufficient score removed." (let ((result nil)) (dolist (word word-list) (when (good-enough-score-p word) (push word result))) result))
Luís Oliveir.. 22
有几种内置的方法可以做到这一点.一种方法是:
(remove-if-not 'good-enough-score-p word-list)
而另一个:
(loop for word in word-list when (good-enough-score-p word) collect word)
而另一个:
(mapcan (lambda (word) (when (good-enough-score-p word) (list word))) word-list)
等等......还有SERIES和Iterate.Iterate版本与LOOP版本相同,但SERIES版本很有趣:
(collect (choose-if 'good-enough-score-p (scan word-list))))
所以,是的,你很可能会重新发明一些轮子.:-)
有几种内置的方法可以做到这一点.一种方法是:
(remove-if-not 'good-enough-score-p word-list)
而另一个:
(loop for word in word-list when (good-enough-score-p word) collect word)
而另一个:
(mapcan (lambda (word) (when (good-enough-score-p word) (list word))) word-list)
等等......还有SERIES和Iterate.Iterate版本与LOOP版本相同,但SERIES版本很有趣:
(collect (choose-if 'good-enough-score-p (scan word-list))))
所以,是的,你很可能会重新发明一些轮子.:-)
您想要的功能remove-if-not
是内置的.
(defun remove-low-words (word-list) (remove-if-not #'good-enough-score-p word-list))
如果你觉得你正在重新发明与列表有关的东西,你可能就是这样.检查Hyperspec以查看.
关于Hyperspec的文档 remove-if-not
所有序列功能
所有特定于列表的功能