From 4398d51300777f367c01ef6b88a664b8dfe733b2 Mon Sep 17 00:00:00 2001 From: jim Date: Fri, 20 Feb 2015 11:54:27 -0500 Subject: [PATCH 1/1] more cleanup --- exercises/_assignment4.mdwn | 76 +++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/exercises/_assignment4.mdwn b/exercises/_assignment4.mdwn index fd11e761..4dfd5dbf 100644 --- a/exercises/_assignment4.mdwn +++ b/exercises/_assignment4.mdwn @@ -39,24 +39,26 @@ For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~> fac 4 5. For this question, we want to implement **sets** of numbers in terms of lists of numbers, where we make sure as we construct those lists that they never contain a single number more than once. (It would be even more efficient if we made sure that the lists were always sorted, but we won't try to implement that refinement here.) To enforce the idea of modularity, let's suppose you don't know the details of how the lists are implemented. You just are given the functions defined below for them (but pretend you don't see the actual definitions). These define lists in terms of [[one of the new encodings discussed last week|/topics/week3_more_lists_]]. - + ; all functions from the previous question, plus - num_equal? = ??? - empty = \f n. n - cons = \x xs. \f n. f x xs - take_while = Y (\take_while. \p xs. xs (\y ys. (p y) (cons y (take_while p ys)) empty) empty) - drop_while = Y (\drop_while. \p xs. xs (\y ys. (p y) (drop_while p ys) ys) empty) + let num_equal? = ??? in + let neg = \b y n. b n y in + let empty = \f n. n in + let cons = \x xs. \f n. f x xs in + let take_while = Y (\take_while. \p xs. xs (\y ys. (p y) (cons y (take_while p ys)) empty) empty) in + let drop_while = Y (\drop_while. \p xs. xs (\y ys. (p y) (drop_while p ys) xs) empty) in + ... - The functions `take_while` and `drop_while` work as described in [[Week 1's homework|assignement1]]. + The functions `take_while` and `drop_while` work as described in [[Week 1's homework|assignment1]]. - Using those resources, define a `set_cons` and a `set_equal?` function. The first should take a number argument `x` and a set argument `xs` (implemented as a list of numbers assumed to have no repeating elements), and return a (possibly new) set argument `xs'` which contains `x`. (But make sure `x` doesn't appear in the result twice!) The `set_equal?` function should take two set arguments `xs` and `ys` and say whether they represent the same set. (Be careful, the lists `[1, 2]` and `[2, 1]` are different lists but do represent the same set. Hence, you can't just use the `list_equal?` function you defined in last week's homework.) + Using those resources, define a `set_cons` and a `set_equal?` function. The first should take a number argument `x` and a set argument `xs` (implemented as a list of numbers assumed to have no repeating elements), and return a (possibly new) set argument which contains `x`. (But make sure `x` doesn't appear in the result twice!) The `set_equal?` function should take two set arguments `xs` and `ys` and say whether they represent the same set. (Be careful, the lists `[1, 2]` and `[2, 1]` are different lists but do represent the same set. Hence, you can't just use the `list_equal?` function you defined in last week's homework.) - Here are some tips for getting started. Use `drop_while` and `num_equal?` to define a `mem?` function that returns `true` if ` number `x` is a member of a list of numbers `xs`, else returns `false`. Also use `take_while` and `drop_while` to define a `without` function that returns a copy of a list of numbers `xs` that omits the first occurrence of a number `x`, if there be such. You may find these functions `mem?` and `without` useful in defining `set_cons` and `set_equal?`. Also, for `set_equal?`, you are probably going to want to define the function recursively... as now you know how to do. + Here are some tips for getting started. Use `drop_while` and `num_equal?` to define a `mem?` function that returns `true` if number `x` is a member of a list of numbers `xs`, else returns `false`. Also use `take_while` and `drop_while` to define a `without` function that returns a copy of a list of numbers `xs` that omits the first occurrence of a number `x`, if there be such. You may find these functions `mem?` and `without` useful in defining `set_cons` and `set_equal?`. Also, for `set_equal?`, you are probably going to want to define the function recursively... as now you know how to do. 6. Questions about trees. @@ -116,7 +118,7 @@ part of the work to another function `odd?`: ; else result turns on whether x-1 is odd (odd? (pred x)) -At the same tme, though, it's natural to define `odd?` in such a way that it + At the same tme, though, it's natural to define `odd?` in such a way that it hands off part of the work to `even?`: let odd? = \x. (zero? x) @@ -125,41 +127,41 @@ hands off part of the work to `even?`: ; else result turns on whether x-1 is even (even? (pred x)) -Such a definition of `even?` and `odd?` is called **mutually recursive**. If you + Such a definition of `even?` and `odd?` is called **mutually recursive**. If you trace through the evaluation of some sample numerical arguments, you can see that eventually we'll always reach a base step. So the recursion should be perfectly well-grounded: - even? 3 - ~~> (zero? 3) true (odd? (pred 3)) - ~~> odd? 2 - ~~> (zero? 2) false (even? (pred 2)) - ~~> even? 1 - ~~> (zero? 1) true (odd? (pred 1)) - ~~> odd? 0 - ~~> (zero? 0) false (even? (pred 0)) - ~~> false - -But we don't yet know how to implement this kind of recursion in the Lambda + even? 3 + ~~> (zero? 3) true (odd? (pred 3)) + ~~> odd? 2 + ~~> (zero? 2) false (even? (pred 2)) + ~~> even? 1 + ~~> (zero? 1) true (odd? (pred 1)) + ~~> odd? 0 + ~~> (zero? 0) false (even? (pred 0)) + ~~> false + + But we don't yet know how to implement this kind of recursion in the Lambda Calculus. -The fixed point operators we've been working with so far worked like this: + The fixed point operators we've been working with so far worked like this: - let ξ = Y h in - ξ <~~> h ξ + let ξ = Y h in + ξ <~~> h ξ -Suppose we had a pair of fixed point operators, `Y1` and `Y2`, that operated on + Suppose we had a pair of fixed point operators, `Y1` and `Y2`, that operated on a *pair* of functions `h` and `g`, as follows: - let ξ1 = Y1 h g in - let ξ2 = Y2 h g in - ξ1 <~~> h ξ1 ξ2 and - ξ2 <~~> g ξ1 ξ2 + let ξ1 = Y1 h g in + let ξ2 = Y2 h g in + ξ1 <~~> h ξ1 ξ2 and + ξ2 <~~> g ξ1 ξ2 -If we gave you such a `Y1` and `Y2`, how would you implement the above + If we gave you such a `Y1` and `Y2`, how would you implement the above definitions of `even?` and `odd?`? -11. (More challenging.) Using our derivation of Y from [[this week's notes|topics/week4_fixed_point_combinators_]] as a model, construct a pair `Y1` and `Y2` that behave in the way described. +11. (More challenging.) Using our derivation of `Y` from [[this week's notes|topics/week4_fixed_point_combinators_]] as a model, construct a pair `Y1` and `Y2` that behave in the way described above. - Here is one hint to get you started: remember that in the notes, we constructed a fixed point for `h` by evolving it into `H` and using `H H` as `h`'s fixed point. We suggested the thought exercise, how might you instead evolve `h` into some `T` and then use `T T T` as `h`'s fixed point. Try solving this problem first. It may help give you the insights you need to define a `Y1` and `Y2` that work as described above. + Here is one hint to get you started: remember that in the notes, we constructed a fixed point for `h` by evolving it into `H` and using `H H` as `h`'s fixed point. We suggested the thought exercise, how might you instead evolve `h` into some `T` and then use `T T T` as `h`'s fixed point. Try solving this problem first. It may help give you the insights you need to define a `Y1` and `Y2`. -- 2.11.0