[itn book] map+curry+inc unexpected behaviour on v10.3.3

Started by conan, December 22, 2011, 06:36:48 AM

Previous topic - Next topic

conan

I'm reading now the chapter Working with numbers from Introduction to newlisp book, and in the third code example under the subtitle Invisible conversion and rounding we see:

(set 'lst '(2 6 9 12))
;-> (2 6 9 12)
(inc (lst 0))
;-> 3
lst
;-> (3 6 9 12)
(map inc lst)
;-> (4 7 10 13)
(map (curry inc 3) lst)
;-> (6 9 12 15) ; === CHECK OUT THIS LINE ===
lst
;-> (3 6 9 12)

But on v10.3.3 I get the map+curry operation to work on the list like this instead:

newLISP v.10.3.3 64-bit on Linux IPv4/6 UTF-8, execute 'newlisp -h' for more info.
> (set 'lst '(2 6 9 12))
(2 6 9 12)
> (inc (lst 0))
3
> lst
(3 6 9 12)
> (map inc lst)
(4 7 10 13)
> (map (curry inc 3) lst)
(6 12 21 33) ; === CHECK OUT THIS LINE ===
> lst
(3 6 9 12)

It's taking the number by pairs and adding them instead of increasing them individually by 3 (except the first one).



Now I recall reading about a function that takes elements by pairs, can't recall the name right now, but recalling that it exists confuses me on this behaviour. Is it expected? Is map+curry+inc supposed to take the list elements in pairs and operate them with one another? I feel it shouldn't, but I may be wrong.



Doing...

newLISP v.10.3.3 64-bit on Linux IPv4/6 UTF-8, execute 'newlisp -h' for more info.
> (set 'lst '(2 6 9 12))
(2 6 9 12)
> (map (curry inc 3) lst)
(5 11 20 32)
> lst
(2 6 9 12)
> (map (lambda (x) (+ x 3)) lst)
(5 9 12 15)
> (map (lambda (x) (inc x 3)) lst)
(5 9 12 15)
> (map (curry + 3) lst)
(5 9 12 15)

...works as expected. So now I see the curry+inc is the issue.



What's going on? Should I update ITN book and denote the pitfall as expected behavior or should this behavior be modified?

Lutz

Destructive functions should not be used with curry, but as a workaround, you could do:


> lst
(2 6 9 12)
> (map (curry inc (copy 3)) lst)
(5 9 12 15)


This will be mentioned in the reference manual for 'curry' and should also be mentioned in the WikiBooks introduction.



ps: is now added in the online manual http://www.newlisp.org/downloads/newlisp_manual.html#curry">http://www.newlisp.org/downloads/newlis ... html#curry">http://www.newlisp.org/downloads/newlisp_manual.html#curry

conan

Quote from: "Lutz"Destructive functions should not be used with curry...

This will be mentioned in the reference manual for 'curry' and should also be mentioned in the WikiBooks introduction.


Cool. I'll be updating ITN book in a couple of hours, I need to take a nap now.

jopython

What is the ITN book?

conan

Quote from: "jopython"What is the ITN book?

I mentioned the full title in the first message: ITN book is http://en.wikibooks.org/wiki/Introduction_to_newLISP">Introduction to newLISP book. It's linked in newlisp.org documentation, I thought it was pretty known in the community.

xytroxon

Cormullion's original work was a single html (or pdf) version users downloaded to their machines... The wikibooks version came later... Older users are more familiar of thinking in terms of "the Cormullion html/pdf rather than of "ITN"... With other's adding / editing content though, "ITN" becomes a more proper way to think of it in the future...



-- xytroxon

\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

conan

I just added the notice to avoid using curry with destructive functions in the chapter http://en.wikibooks.org/w/index.php?title=Introduction_to_newLISP/Apply_and_map&stable=0#currying">Apply and map which is the first one to mention curry function.



However I stomp on something when I went to edit the section http://en.wikibooks.org/w/index.php?title=Introduction_to_newLISP/Working_with_numbers&stable=0#Invisible_conversion_and_rounding">Invisible conversion and rounding. See, the thing is cormullion wrote that section to tell about inc and dec converting floats to int when no step was provided. But I noticed newlisp v10.3.3 does increment and decrement without converting floats to ints.



cormullion stated:

(inc 2.5) ; gives 3, not 3.5 as expected

But that's not true anymore on v10.3.3:

(inc 2.5)
3.5

So the question is:



A.- Should I erase the whole section since the behavior has changed?

B.- Or are there other functions that still make the conversion from float to int and can be used to exemplify the issue?

Lutz

Since stable release 10.2.0 (developed in 10.1.10) inc and dec always produce floats and we have the new ++ and -- which produce integers. Now consistently all operators made of letters of the alphabet produce floats and operators written with special characters pruduce ints.



http://www.newlisp.org/downloads/previous-release-notes/newLISP-10.2-Release.html">http://www.newlisp.org/downloads/previo ... lease.html">http://www.newlisp.org/downloads/previous-release-notes/newLISP-10.2-Release.html

conan

Thanks, I used ++ to keep the section. Also modified it a little bit to make emphasis on the implicit conversion both inc/dec and ++/-- do.