Stefan O'Rear wrote:
sum = sum' 0
sum' k [] = k
sum' k (x:xs) = (sum' $! (k+x)) xs

enum x y | x >= y    = 0
         | otherwise = x : enum (x+1) y


sum (enum 1 10)                 =>
sum' 0 (enum 1 10)              =>
sum' 0 (1 : enum (1+1) 10)      =>
(sum' $! (0+1)) (enum (1+1) 10) =>
sum' 1 (enum (1+1) 10)          =>

sum' 1 (2 : enum (2+1) 10)      =>
(sum' $! (1+2)) (enum (2+1) 10) =>
sum' 3 (enum (2+1) 10)          =>

sum' 3 (3 : enum (3+1) 10)      =>
(sum' $! (3+3)) (enum (3+1) 10) =>
sum' 6 (enum (3+1) 10)          =>

sum' 6 (4 : enum (4+1) 10)      =>
(sum' $! (6+4)) (enum (4+1) 10) =>
sum' 10 (enum (4+1) 10)         =>

...


sum' 36 (9 : enum (9+1) 10)      =>
(sum' $! (36+9)) (enum (9+1) 10) =>
sum' 45 (enum (9+1) 10)          =>
sum' 45 []                       =>
45

(I need to find some way to automate making these trails :) )

I did have a fairly small Tcl implementation for this...

I don't have the code now, and I wrote it early in my Haskell career, so there's masses of stuff it didn't handle. (*cough* type classes)

Actually, I've often longed for some tool (maybe even integrated into Lambdabot) to show the reduction sequence of an arbitrary expression. But none exists, AFAIK...

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to