strange result with map and apply

Q&A's, tips, howto's
Locked
fdb
Posts: 66
Joined: Sat Nov 09, 2013 8:49 pm

strange result with map and apply

Post by fdb »

Code: Select all

> (map (apply +) '((0 1 2) (3 4 5)))
((0 1 2) (3 4 5))
> (map (fn(x) (apply + x)) '((0 1 2) (3 4 5)))
(3 12)
Why doesn't the first example work?

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: strange result with map and apply

Post by bairui »

With:

Code: Select all

(map (apply max) '((0 1 2) (3 4 5)))
we see the expected error:

ERR: missing argument in function max

I don't know why we don't see this with the + function, but the solution to both cases is to use curry:

Code: Select all

(map (curry apply +) '((0 1 2) (3 4 5)))

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

Re: strange result with map and apply

Post by Lutz »

Like in Scneme (+) or (apply +) evaluates to 0. So your map expression maps 0 onto two lists.
Using a number as operator is implicit resting. 0 gives you back the whole list.
Try also (map (apply *) '((0 1 2) (3 4 5))) to prove the point. Like in Scheme (*) gives 1.

So using curry for what you want is the right thing.

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: strange result with map and apply

Post by bairui »

Ah, that makes sense. Thanks, Lutz.

fdb
Posts: 66
Joined: Sat Nov 09, 2013 8:49 pm

Re: strange result with map and apply

Post by fdb »

Thanks a lot bairui and Luts! Hadn't used curry before.

Another question i just encountered is how to change all items in a list in one go, for example:

Code: Select all

(set-ref-all "2" '(("1" "2" "3") ("4")) (int $it))
(("1" 2 "3") ("4"))
Works but i want to change all the strings to integers in one go but couldn't find an expression which matches
all the strings, see below some examples i tried but didn't work

Code: Select all

> (set-ref-all '((*)) '(("1" "2" "3") ("4")) (int $it))
nil
> (set-ref-all '(?) '(("1" "2" "3") ("4")) (int $it))
nil
> (set-ref-all '(*) '(("1" "2" "3") ("4")) (int $it))
nil
> (set-ref-all '(+) '(("1" "2" "3") ("4")) (int $it))
nil
> (set-ref-all ? '(("1" "2" "3") ("4")) (int $it))
nil
> (set-ref-all * '(("1" "2" "3") ("4")) (int $it))
nil


cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: strange result with map and apply

Post by cormullion »

How about:

Code: Select all

(set-ref-all '(*) '(("1" "2" "3") ("4")) (map int $it) match)
;-> ((1 2 3) (4))
I admit that match is the one area in newLISP that I always fail to get right in the first few attempts... :)

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: strange result with map and apply

Post by bairui »

Not as veratile as cormullion's match solution, but works for the given example:

Code: Select all

(map (curry map int) '(("1" "2" "3") ("4")))

fdb
Posts: 66
Joined: Sat Nov 09, 2013 8:49 pm

Re: strange result with map and apply

Post by fdb »

Thx!

I understood the function from bairui directly (why didn't i think of that..) but i had to look in the documentation to understand cormullion's solution and then i saw in the documentation this example:
(set-ref-all '(oranges *) data (list (first $it) (apply + (rest $it))) match) ....

Great docs, just have to read them more carefully next time.

Locked