X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?a=blobdiff_plain;f=advanced_topics%2Fcalculator_improvements.mdwn;fp=advanced_topics%2Fcalculator_improvements.mdwn;h=4f24f877c52e21ab64439e2d39e6f53add7946fc;hb=88f7b0444146d9379521cc31ad9ef87b909e0b3b;hp=e22afb1aa62904e601ee7ef17e774317b0335fb1;hpb=3ae2b6a91a7bacea08d522cb94f78b2c7b2d95c9;p=lambda.git diff --git a/advanced_topics/calculator_improvements.mdwn b/advanced_topics/calculator_improvements.mdwn index e22afb1a..4f24f877 100644 --- a/advanced_topics/calculator_improvements.mdwn +++ b/advanced_topics/calculator_improvements.mdwn @@ -120,7 +120,7 @@ We can begin with our language: | 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 @@ -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: - 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: