From: Jim Pryor Date: Fri, 26 Nov 2010 02:53:42 +0000 (-0500) Subject: tweak calc improvements X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=1fd1fc4930f5d25e7de28532cdefbe103d4b3a1b;hp=bd90f2eb8b87c2f44169fe209c5e35dd113c3d21 tweak calc improvements Signed-off-by: Jim Pryor --- diff --git a/advanced_topics/calculator_improvements.mdwn b/advanced_topics/calculator_improvements.mdwn index d6654db6..3e51c95b 100644 --- a/advanced_topics/calculator_improvements.mdwn +++ b/advanced_topics/calculator_improvements.mdwn @@ -61,12 +61,12 @@ We'd then want to add the ability to construct pairs, and extract their componen We won't try here to catch any type errors, such as attempts to add a `bool` to an `int`, or attempts to check whether a `bool` iszero. Neither will we try here to monadize anything: these will be implementations of a calculator with all the plumbing exposed. What we will do is add more and more features to the calculator. -We'll switch over to using variable `g` for assignment functions, which is a convention many of you seem familiar with. As we mentioned a few times in week 9, for some purposes it's easier to implement environment or assignment functions as functions from `char`s to `int`s (or whatever variables are bound to), rather than as lists of pairs. However, we'll stick with this implementation for now. We will however abstract out the type that the variables are bound to. For now, we'll suppose that they're bound to the same types that terms can express. +We'll switch over to using variable `g` for assignment functions, which is a convention many of you seem familiar with. As we mentioned a few times in [[week9]], for some purposes it's easier to implement environment or assignment functions as functions from `char`s to `int`s (or whatever variables are bound to), rather than as lists of pairs. However, we'll stick with this implementation for now. We will however abstract out the type that the variables are bound to. For now, we'll suppose that they're bound to the same types that terms can express. type bound_value = expressed_value;; type assignment = (char * bound_value) list;; -Here's where we should be now. We expand some of the clauses in the `eval` function for clarity, and we rename a few variables: +Here's where we should be now. We'll work with the language: type term = Intconstant of int @@ -80,6 +80,8 @@ Here's where we should be now. We expand some of the clauses in the `eval` funct | First of term ;; +Here is our evaluation function. We expand some of the clauses and rename a few variables for clarity. Our implementation should make it clear how to add additional constants or native predicates, such as a `Second` predicate for extracting the second element of a pair. + let rec eval (t : term) (g : assignment) = match t with Intconstant x -> Int x | Multiplication (t1, t2) ->