add hints
authorjim <jim@web>
Sat, 21 Mar 2015 01:02:53 +0000 (21:02 -0400)
committerLinux User <ikiwiki@localhost.members.linode.com>
Sat, 21 Mar 2015 01:02:53 +0000 (21:02 -0400)
exercises/assignment5_answers.mdwn

index 7388948..34a7dda 100644 (file)
@@ -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.)
 
 
 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:
 
     <pre>
 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:
 
     <pre>
@@ -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]].
     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?