Subject: Re: ITERATE vs LOOP - Popular opinion observation?
From: rpw3@rpw3.org (Rob Warnock)
Date: Thu, 03 Aug 2006 23:52:49 -0500
Newsgroups: comp.lang.lisp
Message-ID: <G7qdnSkkR5E8T0_ZnZ2dnUVZ_oidnZ2d@speakeasy.net>
Pascal Costanza  <pc@p-cos.net> wrote:
+---------------
| List comprehensions, or similar declarative abstractions, would address 
| them.  Indeed that's how I try to use LOOP. When I say this:
|     (loop for elem in list
|            when (some-predicate elem)
|            collect elem)
| ...I try not to understand this as an iteration, but as a declarative 
| expression of the result I want.
+---------------

And to my taste, that's *much* cleaner looking and
more comprehensible [pardon the pun!] six months later
than anything I can think of writing with MAPxxx.

The combination of LOOP...WHEN...COLLECT work particularly
well together, especially when there are multiple WHEN...COLLECT
sub-expressions. Here's an admittedly-ugly [but useful] piece
of code. Would a MAPxxx version be more readable?

    ;;; Spray bits out for easy reading of hardware registers
    (defun decode-bits (n)
      (let ((mflag (minusp n)))
	(when mflag
	  (setf n (lognot n)))
	(loop for i downfrom (integer-length n) to 0
	  when mflag
	    collect (if (zerop n) 'all-ones 'all-ones-except)
	    and do (setf mflag nil)
	  when (logbitp i n)
	    collect i)))

Usage:

    > (decode-bits 16542)

    (14 7 4 3 2 1)
    > (decode-bits -16542)

    (ALL-ONES-EXCEPT 14 7 4 3 2 0)
    > (decode-bits -1)

    (ALL-ONES)
    > 


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607