Lisp Style question label local functions or not?
Posted
by Andrew Myers
on Stack Overflow
See other posts from Stack Overflow
or by Andrew Myers
Published on 2010-03-08T18:01:39Z
Indexed on
2010/03/08
18:21 UTC
Read the original article
Hit count: 599
I was wondering if there is a standard practice regarding the use of labels in Lisp. I've been messing around with a Lisp implementation of the algorithm described in the first answer here http://stackoverflow.com/questions/352203/generating-permutations-lazily My current version uses labels to break out portions of functionality.
(defun next-permutation (pmute)
(declare (vector pmute))
(let ((len (length pmute)))
(if (> len 2)
(labels ((get-pivot ()
(do ((pivot (1- len) (1- pivot)))
((or (= pivot 0)
(< (aref pmute (1- pivot))
(aref pmute pivot)))
pivot)))
(get-swap (pivot)
(let ((swp (1- len)))
(loop for i from (1- len) downto pivot do
(if (or (and (> (aref pmute i)
(aref pmute (1- pivot)))
(< (aref pmute i) (aref pmute swp)))
(< (aref pmute swp) (aref pmute (1- pivot))))
(setf swp i)))
swp))
(next (swp pivot)
(rotatef (aref pmute (1- pivot)) (aref pmute swp))
(reverse-vector pmute pivot (1- len))))
(let ((piv (get-pivot)))
(if (> piv 0)
(next (get-swap piv) piv)
nil))))))
Since each label is only called once I was wondering if this is considered bad practice since the only reason to do it in this case is for aesthetic reasons. I would argue that the current version with labels is clearer but that may go against common wisdom that I'm not aware of, being new to Lisp.
© Stack Overflow or respective owner