Q&A's, tips, howto's
eddier
Posts: 289 Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US
Post
by eddier » Tue Jun 14, 2005 5:18 pm
Suppose
Code: Select all
(define data '((1 2) (2 3) (3 4)))
(println (map first data))
=>
Okydoky! That's what I expect. But,
=>
and
=>
Looks like (map
n list ) means appling (
n sublist) instead of (sublist
n ).
Shouldn't this be the other way around? That
and
?
Eddie[/i]
Lutz
Posts: 5289 Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:
Post
by Lutz » Tue Jun 14, 2005 8:34 pm
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 » Tue Jun 14, 2005 8:42 pm
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 » Tue Jun 14, 2005 8:52 pm
'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