
I stumbled across this example as a shiny way of explaining why Lazy eval matters.... fact = fix $ \f n -> if n == 0 then 1 else n * f (n-1) fix f = f (fix f) With lazy eval, I get fact 4 = fix $ \f 4 (......) since haskell goes outside to inside I have, fac 4 = $ \f 4 (......) (fix $ \f 4 (......)) Now the first chunk can get evaluated, but what about the rest of the expression. The outer "f" from "fix f" function has the parameter (fix f). So when writing "f (fix f)" apparently f got evaluated without using this parameter. I understand that the anonymous lambda does have 4 as the param, but somehow "f (fix f)" doesn't feel complete. -Animesh

On Mon, Jan 26, 2015, at 08:43 PM, Animesh Saxena wrote:
I stumbled across this example as a shiny way of explaining why Lazy eval matters....
fact = fix $ \f n -> if n == 0 then 1 else n * f (n-1) fix f = f (fix f)
With lazy eval, I get
fact 4 = fix $ \f 4 (......)
Not quite. Let's go one step at a time: fact 4 = (fix $ \f n -> if n == 0 then 1 else n * f (n - 1)) 4 Now we can bring in the definition of fix, substituting the argument to fix for all of the occurrences of f in the definition of fix. Notice that we can't substitute the 4 for n yet. fact 4 = ((\f n -> if n == 0 then 1 else n * f (n - 1)) (fix $ \f n -> if n == 0 then 1 else n * f (n - 1))) 4 I think if you are very patient and methodical about performing the substitutions (and writing them out fully - no dot-dot-dot), then you'll figure out how it all works. -Karl

Take a look here
http://bm380.user.srcf.net/cgi-bin/stepeval.cgi?expr=let+fix+f+%3D+f+%28fix+...
.
The above webpage uses stepval https://github.com/bmillwood/stepeval, it
can evaluate any expression step by step using equational reasoning.
On 27 January 2015 at 11:54, Karl Voelker
On Mon, Jan 26, 2015, at 08:43 PM, Animesh Saxena wrote:
I stumbled across this example as a shiny way of explaining why Lazy eval matters....
fact = fix $ \f n -> if n == 0 then 1 else n * f (n-1) fix f = f (fix f)
With lazy eval, I get
fact 4 = fix $ \f 4 (......)
Not quite. Let's go one step at a time:
fact 4 = (fix $ \f n -> if n == 0 then 1 else n * f (n - 1)) 4
Now we can bring in the definition of fix, substituting the argument to fix for all of the occurrences of f in the definition of fix. Notice that we can't substitute the 4 for n yet.
fact 4 = ((\f n -> if n == 0 then 1 else n * f (n - 1)) (fix $ \f n -> if n == 0 then 1 else n * f (n - 1))) 4
I think if you are very patient and methodical about performing the substitutions (and writing them out fully - no dot-dot-dot), then you'll figure out how it all works.
-Karl
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Regards Sumit Sahrawat
participants (3)
-
Animesh Saxena
-
Karl Voelker
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)