strange mapping

Started by eddier, June 14, 2005, 10:18:31 AM

Previous topic - Next topic

eddier

Suppose

(define data '((1 2) (2 3) (3 4)))
(println (map first data))

=>
(1,2,3)

Okydoky! That's what I expect. But,


(println (map 0 data))
=>
((1 2) (2 3) (3 4))

and


(println (map 1 data))
=>
((2) (3) (4))

Looks like (map n list) means appling (n sublist) instead of (sublist n).

Shouldn't this be the other way around? That
(map 0 data) => (1 2 3) and
(map 1 data) => (2 3 4)?



Eddie[/i]

Lutz

#1
The implicit indexing form (idx lst) doesn't mean 'nth' but is taking the 'slice' starting at index:



(0 '(1 2 3 4)) => (1 2 3 4)
(1 '(1 2 3 4)) => (2 3 4)

;; or

(1 2 '(1 2 3 4)) => (2 3)


and (map idx lst) behaves accordingly (the first part of the example)



what you mean is the implicit indexing form of 'nth' with the index behind the list:



(set 'data '(1 2 3 4 5))
(data 0) => 1
(data 1) = 2


Lutz



ps: see http://newlisp.org/downloads/newlisp_manual.html#indexing">http://newlisp.org/downloads/newlisp_ma ... l#indexing">http://newlisp.org/downloads/newlisp_manual.html#indexing

eddier

#2
Yes,



I was wondering why doesn't map take implicit indexing instead of implicit slicing?



Would it not be more appropriate for map to take indexing?  Why would we ever want map to be slicing?



Thanks!



Eddie

Lutz

#3
'map' always applies the first parameter to each element of the followoing list(s) parameter(s). You cannot suddenly change that sequence for the case of implicit indexing.



But you still could do:



(set 'data '(a b c d))

(map 'data '(3 2 1 0)) => (d c b a)


so everything works consistent. Note that data is quoted because 'map' evaluates the 1st parameter before applying it and so does implicit indexing.



mapping slice is useful for stripping all lists in a list of leading elements.



Lutz