Page 1 of 1

strange mapping

Posted: Tue Jun 14, 2005 5:18 pm
by eddier
Suppose

Code: Select all

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

Code: Select all

(1,2,3)
Okydoky! That's what I expect. But,

Code: Select all

(println (map 0 data))
=>

Code: Select all

((1 2) (2 3) (3 4))
and

Code: Select all

(println (map 1 data))
=>

Code: Select all

((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

Code: Select all

(map 0 data) => (1 2 3)
and

Code: Select all

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

Eddie[/i]

Posted: Tue Jun 14, 2005 8:34 pm
by Lutz
The implicit indexing form (idx lst) doesn't mean 'nth' but is taking the 'slice' starting at index:

Code: Select all

(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:

Code: Select all

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

ps: see http://newlisp.org/downloads/newlisp_ma ... l#indexing

Posted: Tue Jun 14, 2005 8:42 pm
by eddier
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

Posted: Tue Jun 14, 2005 8:52 pm
by Lutz
'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:

Code: Select all

(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