
Ah, lambda, function as first order object and curry.
A million thanks, that clears my mind. It is very hard
for me to shake away the things that I have learnt for
over 20 years. I still think from time to time that
the "accumulator" in foldr/foldl as a constant being
folded and forget that it can be anything. In this
case, because 'n' is not binded during the fold, it(g
(x*n)) becomes a function needing one more parameter
and keeps getting folded. The '1' at the end becomes
this final parameter that makes all these stacked
function in function all being evaluated back to
value.
Your explanation really taught me about these concepts
as I can understand simple tutorials(I used them in my
python programs) about them but only this kind deep
recursive I can see the advantage of curry.
I have already gone through the links you suggested
before I tried to see the various factorial
implementation, but I am sure I need to go back to
them again and again as haskell(and the idea behind it
in programming) is not something I can understand in
one go.
IMO, every CS course should teach haskell, as one can
easily pick up C/Pascal etc. without any help but
haskell is all too different.
--- Thomas Spriggs
Well, here goes. The way the lambda function/foldr thing evaluates, the resulting foldl like function needs a new identity parameter hence the additional "1". To demonstrate something like how this is evaluated for a low number eg 3: (Please would someone correct me if I have made a mistake in this) fac 3 = (foldr (\x g n -> g (x*n)) id [1..3]) 1 fac 3 = (foldr (\x g n -> g (x*n)) id [1,2,3]) 1 fac 3 = (foldr (\x g n -> g (x*n)) (\n -> id (3*n)) [1,2])) 1 fac 3 = (foldr (\x g n -> g (x*n)) (\n -> (\n -> id (3*n)) (2*n)) [1]) 1 fac 3 = (foldr (\x g n -> g (x*n)) (\n -> (\n -> (\n -> id (3*n)) (2*n)) (1*n)) []) 1 fac 3 = (\n -> (\n -> (\n -> id (3*n)) (2*n)) (1*n)) 1 fac 3 = (\n -> (\n -> id (3*n)) (2*n)) (1*1) fac 3 = (\n -> (\n -> id (3*n)) (2*n)) 1 fac 3 = (\n -> id (3*n)) (2*1) fac 3 = (\n -> id (3*n)) 2 fac 3 = id (3*2) fac 3 = id 6 fac 3 = 6 I would suggest that you use something other than the "evolution of a haskell programmer" to learn haskell as the versions of factorial get complicated very quickly and its largely use less as you should probably just use: fac n = product [1..n] anyway. A better introduction would be something like http://www.cse.unsw.edu.au/~cs1011/05s2/ and use
http://zvon.org/other/haskell/Outputglobal/index.html
and http://www.haskell.org/tutorial/ if you want to learn something in specific or are strugling. All links from http://www.haskell.org/learning.html of course.
thanks for help in advance.
You're welcome.
regards,
gary
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort. http://store.yahoo.com/redcross-donate3/ _______________________________________________ Haskell-Cafe mailing list askell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Well good luck furthering your knowledge of haskell,
Thomas
_________________________________________________________________
Be the first to hear what's new at MSN - sign up to our free newsletters! http://www.msn.co.uk/newsletters
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com