strange mapping

Q&A's, tips, howto's
Locked
eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

strange mapping

Post 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]

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked