I want to eliminate the double-execution of (condition-on a) in the following source code:
Code: Select all
(loop for a in L
when (consp (condition-on a))
do (return (condition-on a)))
Code: Select all
(loop for a in L
when (consp (condition-on a))
do (return (condition-on a)))
Code: Select all
(loop for a in L
for condition = (condition-on a)
when (consp condition)
do (return condition))
Code: Select all
(loop for f in g
when (>= f tr)
for fc = (foo f)
when (consp fc)
collecting fc))
Code: Select all
(loop for f in g
as fc = (and (>= f tr) (foo f))
when (consp fc)
collect fc)
Code: Select all
(let ((results nil))
(dolist (f g (nreverse results))
(when (>= f tr)
(let ((fc (foo f)))
(when (consp fc) (push fc results)))))
Code: Select all
(defun process (f)
(when (> f tr)
(let ((fc (foo f)))
(and (consp fc) (list fc)))))
(mapcan #'process g)
Thanks, but with this I get "Error: The variable WHEN is unbound."Warren Wilkinson wrote:A couple of ways
Code: Select all
(loop for f in g as fc = (and (>= f tr) (foo f)) when (consp fc) collect fc)
Code: Select all
(defun foo (f) (if (evenp f) (list f) f))
(loop for f in '(1 2 3 4)
as fc = (and (>= f 2) (foo f))
when (consp fc)
collect fc)
;; gives ==> ((2) (4))