X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=code%2Fcalculator%2Fcalc4.ml;h=1343827d69844c6bd5386af8f76ac2dd7dc8d62b;hp=a04587b86291b3fc143ccd6f2400e9ce6dfc88ce;hb=573a8b36ce653c84c2aecb2b81ef99128cb41d13;hpb=38f178b5967f0875383d87a92330ecd58979f83e diff --git a/code/calculator/calc4.ml b/code/calculator/calc4.ml index a04587b8..1343827d 100644 --- a/code/calculator/calc4.ml +++ b/code/calculator/calc4.ml @@ -1,4 +1,4 @@ -(* calc3.ml, enhanced with Mutable Cells *) +(* calc4.ml: calc3.ml enhanced with Mutable Cells *) type term = Intconstant of int @@ -88,12 +88,12 @@ in let g' = (var_to_bind, Recursive_Closure (var_to_bind, arg_var, body, savedg)) :: g in eval t3 g' s' | Newref (t1) -> - let (starting_val, s') = eval t1 g s + let (value1, s') = eval t1 g s (* note that s' may be different from s, if t1 itself contained any mutation operations *) (* now we want to retrieve the next free index in s' *) in let new_index = List.length s' - (* now we want to insert starting_val there; the following is an easy but inefficient way to do it *) - in let s'' = List.append s' [starting_val] + (* now we want to insert value1 there; the following is an easy but inefficient way to do it *) + in let s'' = List.append s' [value1] (* now we return a pair of a wrapped new_index, and the new store *) in (Mutcell new_index, s'') | Deref (t1) -> @@ -105,12 +105,12 @@ (* we don't handle cases where t1 doesn't evaluate to a Mutcell *) let (Mutcell index1, s') = eval t1 g s (* note that s' may be different from s, if t1 itself contained any mutation operations *) - in let (new_value, s'') = eval t2 g s' - (* now we create a list which is just like s'' except it has new_value in index1 *) + in let (value2, s'') = eval t2 g s' + (* now we create a list which is just like s'' except it has value2 in index1 *) in let rec replace_nth lst m = match lst with | [] -> failwith "list too short" - | x::xs when m = 0 -> new_value :: xs + | x::xs when m = 0 -> value2 :: xs | x::xs -> x :: replace_nth xs (m - 1) in let s''' = replace_nth s'' index1 (* we'll arbitrarily return Int 42 as the expressed_value of a Setref operation *)