Date: Fri, 20 Mar 2015 21:02:53 -0400
Subject: [PATCH] add hints
---
exercises/assignment5_answers.mdwn | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
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?
--
2.11.0