Re: [Chicken-users] Problem with (fold)

2014-03-11 Thread Peter Bex
On Tue, Mar 11, 2014 at 03:47:53PM +0100, Daniel Carrera wrote:
> On 11 March 2014 15:41, Peter Bex  wrote:
> 
> > To avoid such mistakes, it's helpful to use mnemonic names:
> >
> > (fold (lambda (item result) (+ (* result 10) item)) 0 '(1 2 3))
> >
> >
> Thanks. I was mentally reading from left to right, so I ended up assuming
> that it was (result item).

That would make more sense, I guess, and is the ordering used by other
functional languages.  There's a built-in version of fold which obeys
this ordering, called foldl:
http://api.call-cc.org/doc/library/foldl

I think SRFI-1's FOLD uses the oher way around is because of the
notion that the procedure argument is called a CONStructor, and "cons"
accepts the item first and the list on which to cons second, making
"reverse" trivial to implement like this:

(fold cons '() '(1 2 3 4)) => (4 3 2 1)

I find it's easy enough to remember if you keep this in mind.

Cheers,
Peter
-- 
http://www.more-magic.net

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Problem with (fold)

2014-03-11 Thread Daniel Carrera
On 11 March 2014 15:41, Peter Bex  wrote:

> To avoid such mistakes, it's helpful to use mnemonic names:
>
> (fold (lambda (item result) (+ (* result 10) item)) 0 '(1 2 3))
>
>
Thanks. I was mentally reading from left to right, so I ended up assuming
that it was (result item).

Cheers,
Daniel.
-- 
When an engineer says that something can't be done, it's a code phrase that
means it's not fun to do.
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Problem with (fold)

2014-03-11 Thread Peter Bex
On Tue, Mar 11, 2014 at 03:30:56PM +0100, Daniel Carrera wrote:
> Hello,
> 
> I'm having a problem with (fold):
> 
> 
> (use srfi-1)  ; List library.
> 
> (fold (lambda (a b) (+ (* a 10) b)) 0 '(1 2 3))
> 
> I was expecting this to return 123, but it returns 60. I'm confused. In my
> mind, at each step I shift the current value to the left (i.e. multiply by
> 10) and add the new digit. So the steps should be:
> 
> 1 , 2 --> 10 + 1 = 12
> 12 , 3 --> 120 + 3 = 123
> 
> What am I missing?

You need to multiply the "memo" by 10, not the item:

#;1> (use srfi-1)
#;2> (fold (lambda (a b) (print `(+ (* ,a 10) ,b)) (+ (* a 10) b)) 0 '(1 2 3))
(+ (* 1 10) 0)
(+ (* 2 10) 10)
(+ (* 3 10) 30)
60
#;3> (fold (lambda (a b) (+ (* b 10) a)) 0 '(1 2 3))
123

To avoid such mistakes, it's helpful to use mnemonic names:

(fold (lambda (item result) (+ (* result 10) item)) 0 '(1 2 3))

Cheers,
Peter
-- 
http://www.more-magic.net

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Problem with (fold)

2014-03-11 Thread Daniel Carrera
I think I've answered my own question: I have to switch the "a" and the "b".

I guess I was confused as to how parameters are sent into the lambda.

Cheers,
Daniel.


On 11 March 2014 15:30, Daniel Carrera  wrote:

> Hello,
>
> I'm having a problem with (fold):
>
>
> (use srfi-1)  ; List library.
>
> (fold (lambda (a b) (+ (* a 10) b)) 0 '(1 2 3))
>
>
> I was expecting this to return 123, but it returns 60. I'm confused. In my
> mind, at each step I shift the current value to the left (i.e. multiply by
> 10) and add the new digit. So the steps should be:
>
> 1 , 2 --> 10 + 1 = 12
> 12 , 3 --> 120 + 3 = 123
>
> What am I missing?
>
> Cheers,
> Daniel.
> --
> When an engineer says that something can't be done, it's a code phrase
> that means it's not fun to do.
>



-- 
When an engineer says that something can't be done, it's a code phrase that
means it's not fun to do.
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


[Chicken-users] Problem with (fold)

2014-03-11 Thread Daniel Carrera
Hello,

I'm having a problem with (fold):


(use srfi-1)  ; List library.

(fold (lambda (a b) (+ (* a 10) b)) 0 '(1 2 3))


I was expecting this to return 123, but it returns 60. I'm confused. In my
mind, at each step I shift the current value to the left (i.e. multiply by
10) and add the new digit. So the steps should be:

1 , 2 --> 10 + 1 = 12
12 , 3 --> 120 + 3 = 123

What am I missing?

Cheers,
Daniel.
-- 
When an engineer says that something can't be done, it's a code phrase that
means it's not fun to do.
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users