[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: MAP (and variants) vs LOOP - Popular opinion observation?

William James

4/10/2015 5:36:00 PM

Pascal Costanza wrote:

> different kinds of values. Recently, I needed the following costruct
> quite often:
>
> (loop for x in some-list
> for i from 0
> collect `(,x ,i))

Gauche Scheme:

(define some-list '(a b c))

(map list some-list (lrange 0))
===>
((a 0) (b 1) (c 2))

Another way:

(use gauche.sequence)

(map-with-index (.$ reverse list) some-list)
===>
((a 0) (b 1) (c 2))

Another way:

(use srfi-42)

(list-ec (: x (index i) some-list) (list x i))


>
> This enumerates all elements in a list. You would have to express this
> completely manually without LOOP because none of the mapxyz functions
> help you here.
>
> - The mapcan idiom to filter out certain elements is, IMHO, a hack. Compare:
>
> (mapcan (lambda (x)
> (when (some-property-p x)
> (list x)))
> some-list)
>
> (loop for x in some-list
> when (some-property-p x)
> collect x)
>
> In this case, LOOP is more concise, and more importantly more explicit
> about what it actually does. To me, that's in fact the most important

If you want to filter, then use filter. Duh.

(filter some-property? some-list)