push assignment2
[lambda.git] / exercises / assignment2.mdwn
diff --git a/exercises/assignment2.mdwn b/exercises/assignment2.mdwn
new file mode 100644 (file)
index 0000000..946df8f
--- /dev/null
@@ -0,0 +1,172 @@
+Syntax
+------
+
+Insert all the implicit `( )`s and <code>&lambda;</code>s into the following abbreviated expressions.
+
+1.  `x x (x x x) x`
+2.  `v w (\x y. v x)`
+3.  `(\x y. x) u v`
+4.  `w (\x y z. x z (y z)) u v`
+
+Mark all occurrences of `(x y)` in the following terms:
+
+<small>(I know the numbering of the homework problems will restart instead of continuing with 5, 6, 7, ... It's too much of a pain to fix it right now. We'll put in a better rendering engine later that will make this work right without laborious work-arounds on our part. Please just renumber the problems appropriately)</small>
+
+5.  `(\x y. x y) x y`
+6.  `(\x y. x y) (x y)`
+7.  `\x y. x y (x y)`
+
+Reduction
+---------
+
+Find "normal forms" for the following---that is, reduce them until no more reductions are possible. As mentioned in the notes, we'll write <code>&lambda;x</code> as `\x`. If we ever say "reduce" without qualifications, we mean just "beta-reduce" (as opposed to "(beta + eta)-reduce").
+
+8.  `(\x \y. y x) z`
+9.  `(\x (x x)) z`
+10. `(\x (\x x)) z`
+11. `(\x (\z x)) z`
+12. `(\x (x (\y y))) (\z (z z))`
+13. `(\x (x x)) (\x (x x))`
+14. `(\x (x x x)) (\x (x x x))`
+
+
+
+Booleans
+--------
+
+Recall our definitions of true and false.
+
+>   **true** is defined to be `\t f. t`  
+>   **false** is defined to be `\t f. f`
+
+In Racket, these functions can be defined like this:
+
+    (define true (lambda (t) (lambda (f) t)))
+    (define false (lambda (t) (lambda (f) f)))
+
+(Note that they are different from Racket's *primitive* boolean values `#t` and `#f`.)
+
+
+15. Define a `neg` operator that negates `true` and `false`.
+
+    Expected behavior: 
+
+        (((neg true) 10) 20)
+
+    evaluates to 20, and 
+
+        (((neg false) 10) 20)
+
+    evaluates to 10.
+
+16. Define an `or` operator.
+
+17. Define an `xor` operator. If you haven't seen this term before, here's a truth table:
+
+        true   xor  true   == false
+        true   xor  false  == true
+        false  xor  true   == true
+        false  xor  false  == false
+
+
+
+Triples
+-------
+
+Recall our definitions of ordered triples.
+
+>   the triple **(**a**,**b**, **c**)** is defined to be `\f. f a b c`
+
+To extract the first element of a triple t, you write:
+
+    t (\fst snd trd. fst)
+
+Here are some definitions in Racket:
+
+    (define make-triple  (lambda (fst) (lambda (snd) (lambda (trd) (lambda (f) (((f fst) snd) trd))))))
+    (define fst_of_three (lambda (fst) (lambda (snd) (lambda (trd) fst))))
+    (define snd_of_three (lambda (fst) (lambda (snd) (lambda (trd) snd))))
+
+Now we can write:
+
+    (define t (((make-triple 10) 20) 30))
+    (t fst_of_three)  ; will evaluate to 10
+    (t snd_of_three)  ; will evaluate to 20
+
+If you're puzzled by having the triple to the left and the function that
+operates on it come second, think about why it's being done this way: the pair
+is a package that takes a function for operating on its elements *as an
+argument*, and returns *the result of* operating on its elements with that
+function. In other words, the triple is a higher-order function.
+
+
+18. Define the `swap12` function that permutes the elements of a triple. Expected behavior:
+
+        (define t (((make-triple 10) 20) 30))
+        ((t swap12) fst_of_three) ; evaluates to 20
+        ((t swap12) snd_of_three) ; evaluates to 10
+
+    Write out the definition of `swap12` in Racket.
+
+
+19. Define a `dup3` function that duplicates its argument to form a triple
+whose elements are the same.  Expected behavior:
+
+        ((dup3 10) fst_of_three) ; evaluates to 10
+        ((dup3 10) snd_of_three) ; evaluates to 10
+
+20. Define a `dup27` function that makes
+twenty-seven copies of its argument (and stores them in a data structure of
+your choice).
+
+
+Folds and Lists
+---------------
+
+21. Using Kapulet syntax, define `fold_left`.
+
+22. Using Kapulet syntax, define `filter` (problem 7 in last week's homework) in terms of `fold_right`.
+
+23. Using Kapulet syntax, define `&&` in terms of `fold_right`. (To avoid trickiness about the infix syntax, just call it `append`.)
+
+24. Using Kapulet syntax, define `head` in terms of `fold_right`. When applied to a non-empty list, it should give us the first element of that list. When applied to an empty list, let's say it should give us `'error`.
+
+25. We mentioned in the Encoding notes that `fold_left (flipped_cons, []) xs` would give us the elements of `xs` but in the reverse order. That is, this is how we can express `reverse` in terms of `fold_left`. How would you express `reverse` in terms of `fold_right`?
+
+    This problem does have an elegant and concise solution, but it may be hard for you to figure it out. We think it will a useful exercise for you to try, anyway. We'll give a [[hint]]. Don't look at the hint until you've gotten really worked up about the problem. Before that, it probably will just be baffling. If your mind has really gotten its talons into the problem, though, the hint might be just what you need to break it open.
+
+    Even if you don't get the answer, we think the experience of working on the problem, and then understanding the answer when we reveal it, will be satisfying and worhtwhile. It also fits our pedagogical purposes for one of the recurring themes of the class.
+
+<!--
+Hint. Suppose the list we want to reverse is [10, 20, 30]. Applying `fold_right` to this will begin by computing `f 30 z` for some `f` and `z` that we specify. If we made the result of that be something like `30 & blah`, or any larger structure that contained something of that form, it's not clear how we could, using just the resources of `fold_right`, reach down into that structure and replace the `blah` with some other element, as we'd evidently need to, since after the next step we should get `30 & (20 & blah)`. What we'd like instead is something like this:
+
+    30 & < >
+
+Where `< >` isn't some *value* but rather a *hole*. Then with the next step, we want to plug into that hole `20 & < >`, which contains its own hole. Getting:
+
+    30 & (20 & < >)
+
+And so on. That is the key to the solution. The questions you need to answer, to turn this into something executable, are:
+
+1. What is a hole? How can we implement it?
+
+2. What should `f` be, so that the result of `f (20, 30 & < >)`, is `30 & (20 & < >)`?
+
+3. What should `z` be, so that the result of `f (30, z)` is `30 & < >`?
+
+4. At the end of the `fold_right`, we're going to end with something like `30 & (20 & (10 & < >))`. But what we want is `[30, 20, 10]`. How can we turn what we've gotten into what we want?
+
+-->
+
+Numbers
+-------
+
+26. Given that we've agreed to Church's encoding of the numbers:
+
+    <code>0 &equiv; \f z. z</code>  
+    <code>1 &equiv; \f z. f z</code>  
+    <code>2 &equiv; \f z. f (f z)</code>  
+    <code>3 &equiv; \f z. f (f (f z))</code>  
+    <code>...</code>
+
+    How would you express the `succ` function in the Lambda Calculus?