X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=exercises%2Fassignment5_answers.mdwn;h=34a7dda8ccc2a1aea6b476dcba00d8df0d887f65;hp=73889482c1999d37564b214af2e9419da50d9d7f;hb=436617710a6cbfdf5a14be46281f20894ae76e50;hpb=3ff1341e924d1f80409704c0da2ec3659ad46242 diff --git a/exercises/assignment5_answers.mdwn b/exercises/assignment5_answers.mdwn index 73889482..34a7dda8 100644 --- a/exercises/assignment5_answers.mdwn +++ b/exercises/assignment5_answers.mdwn @@ -118,6 +118,16 @@ Choose one of these languages and write the following functions. 7. Write a recursive function to make a copy of a `color_tree` with the same structure and inner branch colors, but where the leftmost leaf is now labeled `0`, the second-leftmost leaf is now labeled `1`, and so on. (Here's a [[hint|assignment5 hint3]], if you need one.) +Hint: Consider this pattern: + + # let rec enumerate_from (t:'a color_tree) counter = match t with + | Leaf x -> (Leaf counter, counter+1) + | Branch (left,col,right) -> let (left',counter') = ... in + let (right',counter'') = ... in + ... + ;; + + 8. (More challenging.) Write a recursive function that makes a copy of a `color_tree` with the same structure and inner branch colors, but replaces each leaf label with the `int` that reports how many of that leaf's ancestors are labeled `Red`. For example, if we give your function a tree:
@@ -498,3 +508,13 @@ and that `bool` is any boolean expression.  Then we can try the following:
     does not terminate.  Incidentally, using the shorter `match bool with true -> yes | false -> no` rather than the longer `let b = bool ... in match b with ...` *would* work as we desire. But your assignment is to control the evaluation order *without* using the special evaluation order properties of OCaml's native `if` or of its `match`. That is, you must keep the `let b = ... in match b with ...` structure in your answer, though you are allowed to adjust what `b`, `y`, and `n` get assigned to.
 
     Here's a [[hint|assignment5 hint1]].
+
+Hint: Use thunks!
+
+Further hint: What does
+
+    let x = (fun () -> 2) in
+    let y = (fun () -> 3) in
+    match true with true -> x | false -> y
+
+evaluate to?