Skip to content

Instantly share code, notes, and snippets.

@lispm
Created October 25, 2016 15:15
Show Gist options
  • Save lispm/9b43d64d1d990695a5cd4a098d370458 to your computer and use it in GitHub Desktop.
Save lispm/9b43d64d1d990695a5cd4a098d370458 to your computer and use it in GitHub Desktop.
; 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))))
@lispm
Copy link
Author

lispm commented Oct 25, 2016

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)))

@kat-co
Copy link

kat-co commented Oct 25, 2016

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?

@lispm
Copy link
Author

lispm commented Oct 25, 2016

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))

@kat-co
Copy link

kat-co commented Oct 25, 2016

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