[clean-list] stupid question

Arie Groeneveld bradypus at xs4all.nl
Tue Dec 11 12:52:52 MET 2007


David Minor schreef:
> I expected it to recurse on isum until x<10, that is isum 999 should equal 9, why does it terminate at 27?
>
I'm not an expert, but AFAIK it goes like this
in a non-strict way:

the termination pattern of isum is guarded by

      | x<10 = x

and this gives a 9 (lsd) in the case of 999, but
before it reaches this stage it already pushed 
two 9's on stack via the other guard:

      | otherwise = ones x + isum (x / 10)

Then your function 'so to say' pops the collected
values from stack and summing them, giving 27 as result.


9 + isum 999/10
9 + 9 + isum 99/10
9 + 9 + 9
        ^
        |
        +-- termination result
9+18
27

It works as expected: summing the digits of a number.


=@@i








More information about the clean-list mailing list