tweak calc improvements
authorJim Pryor <profjim@jimpryor.net>
Thu, 25 Nov 2010 16:34:38 +0000 (11:34 -0500)
committerJim Pryor <profjim@jimpryor.net>
Thu, 25 Nov 2010 16:34:38 +0000 (11:34 -0500)
Signed-off-by: Jim Pryor <profjim@jimpryor.net>
advanced_topics/calculator_improvements.mdwn

index e22afb1..4f24f87 100644 (file)
@@ -120,7 +120,7 @@ We can begin with our language:
                | Lambda of (char * term)
                | Apply of (term * term);;
 
                | Lambda of (char * term)
                | Apply of (term * term);;
 
-Next, we need to expand our stock of `expressed_value`s to include function values as well. How should we think of these? We've several times mentioned the issue of how to handle free variables in a function's body, like the `x` in `lambda y -> y + x`. We'll follow the usual functional programming standard for these (known as "lexical scoping"), which keeps track of what value `x` has in the function expression's lexical environment. That shouldn't get shadowed by any different value `x` may have when the function value is later applied. So:
+Next, we need to expand our stock of `expressed_value`s to include function values as well. How should we think of these? We've several times mentioned the issue of how to handle free variables in a function's body, like the `x` in `lambda y -> y + x`. We'll follow the usual functional programming standard for these (known as "lexical scoping"), which keeps track of what value `x` has in the function declaration's lexical environment. That shouldn't get shadowed by any different value `x` may have when the function value is later applied. So:
 
        let x = 1 in let f = lambda y -> y + x in let x = 2 in apply f 2
 
 
        let x = 1 in let f = lambda y -> y + x in let x = 2 in apply f 2
 
@@ -157,7 +157,7 @@ Now our evaluation function needs two further clauses to interpret the two new e
 
 There are different ways to include recursion in our calculator. First, let's imagine our language expanded like this:
 
 
 There are different ways to include recursion in our calculator. First, let's imagine our language expanded like this:
 
-       let x = 1 in letrec f = lambda y -> if iszero y then x else y * f (y - 1) in f 3
+       let x = 1 in letrec f = lambda y -> if iszero y then x else y * apply f (y - 1) in apply f 3
 
 where the AST would be:
 
 
 where the AST would be: