[Lvlug] iterating over plists in common lisp

Theerasak Photha hanumizzle at gmail.com
Sun Jan 14 03:36:26 EST 2007


I found this recipe while I was trolling around in comp.lang.lisp and
thought it might be useful. It's true that more people use hash tables
these days because they scale better, but Common Lisp is replete with
them and I think they still deserve some attention. I actually thought
of it when I was trying to implement a better way to do a lambda
exercise in the Gigamonkeys Practical Common Lisp.

(loop for (x y) on plist by #'cddr *whatever you want to do*)

The by #'cddr is really important b/c otherwise each step of the loop
will have overlapping bindings for x and y.

e.g., change a plist to an alist

(loop for (x y) on '(:a 1 :b 2 :c 3) by #'cddr collecting (cons x y))


More information about the Lvlug mailing list