Created
October 25, 2016 15:15
-
-
Save lispm/9b43d64d1d990695a5cd4a098d370458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; orig | |
| (defun fl-alpha-cuts(lv alpha) | |
| (let ((i) (n) (fset) (sub_list)) | |
| (setq i 0) | |
| (setq n (length lv)) | |
| (while (< i n) | |
| (progn | |
| (setq fset (eval (nth i lv))) | |
| (setq sub_list (fl-alpha-cut fset alpha)) | |
| (print sub_list) | |
| (setq i (+ 1 i)) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ; better | |
| (defun fl-alpha-cuts (lv alpha) | |
| (dolist (e lv) | |
| (print (fl-alpha-cut (eval e) alpha)))) |
Lists in Lisp are based on linked cons cells. They are not vectors. So using NTH in a LOOP is slow. Just iterate over the list:
(defun fl-alpha-cuts (lv alpha)
(loop for e in lv
for fset = (eval e)
for sub-list = (fl-alpha-cuts fset alpha)
do (print sub-list)))Ah, thank you! So effectively we can get it down to the same level as dolist?
(defun fl-alpha-cuts (lv alpha)
(loop for e in lv
do (print (fl-alpha-cuts (eval e) alpha))))I would enjoy hearing your thoughts on why dolist is preferable here. I find the loop macro difficult to remember, so that's one benefit in my book :) Does dolist outperform or anything?
DOLIST or LOOP are just fine. If one does not need more complex logic, then DOLIST is simpler. But usually I would also prefer to use LOOP.
Alternatively use MAPC:
(defun fl-alpha-cuts (lv alpha)
(mapc (lambda (e)
(print (fl-alpha-cuts (eval e) alpha)))
lv))Thanks very much for the replies, and the opinions!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry if I'm not understanding something; my lisp-fu isn't great yet, but the original example seems very unidiomatic to me? Why not write it like this?
Mind you, I think I like
dolistmore, but the original seems like a misrepresentation of a procedural approach.