X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=exercises%2Fassignment2.mdwn;h=a88463255516dc895eca0b0306de17ac63bad7a5;hp=f0dbfcd4e9df6be6103b60aa64c4319897f03ff2;hb=7888d341776f23849c60cd82ca816ed13e5bc9c2;hpb=b23570f3ffe4fc05de3f291d96b20ce474b1e330;ds=sidebyside diff --git a/exercises/assignment2.mdwn b/exercises/assignment2.mdwn index f0dbfcd4..a8846325 100644 --- a/exercises/assignment2.mdwn +++ b/exercises/assignment2.mdwn @@ -1,7 +1,7 @@ Syntax ------ -Insert all the implicit `( )`s and `λ`s into the following abbreviated expressions. +Insert all the implicit `( )`s and `λ`s into the following abbreviated expressions. Don't just insert them *freely*; rather, provide the official expression, without any notational shortcuts, that is syntactically identical to the form presented. 1. `x x (x x x) x` 2. `v w (\x y. v x)` @@ -34,6 +34,8 @@ Find "normal forms" for the following---that is, reduce them until no more reduc Booleans -------- +For these questions, and the ones on triples below, we're setting them up so as to encourage you to experiment with Racket and to formulate your answer in Scheme/Racket syntax. But you can answer in Lambda Calculus syntax if you prefer. + Recall our definitions of true and false. > **true** is defined to be `\t f. t` @@ -49,15 +51,15 @@ In Racket, these functions can be defined like this: 15. Define a `neg` operator that negates `true` and `false`. - Expected behavior: + Expected behavior: (((neg true) 10) 20) - evaluates to 20, and + evaluates to `20`, and (((neg false) 10) 20) - evaluates to 10. + evaluates to `10`. 16. Define an `or` operator. @@ -75,9 +77,9 @@ Triples Recall our definitions of ordered triples. -> the triple **(**a**,**b**, **c**)** is defined to be `\f. f a b c` +> 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: +To extract the first element of a triple `t`, you write: t (\fst snd trd. fst) @@ -94,7 +96,7 @@ Now we can write: (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 +operates on it come second, think about why it's being done this way: the triple 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. @@ -125,17 +127,19 @@ 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`. +22. Using Kapulet syntax, define `filter` (problem 7 in last week's homework) in terms of `fold_right` and other primitive syntax like `lambda`, `&`, and `[]`. Don't use `letrec`! All the `letrec`-ing that happens should come from the one inside the definition of `fold_right`. -23. Using Kapulet syntax, define `&&` in terms of `fold_right`. (To avoid trickiness about the infix syntax, just call it `append`.) +23. Using Kapulet syntax, define `&&` in terms of `fold_right`. (To avoid trickiness about the infix syntax, just call it `append`.) As with problem 22 (the previous problem), don't use `letrec`! -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`. +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 `'err`. As with problem 22, don't use `letrec`! -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`? +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. So that's how we can express `reverse` in terms of `fold_left`. How would you express `reverse` in terms of `fold_right`? As with problem 22, don't use `letrec`! 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|assignment2 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. + There are also other, less cool answers. Perhaps you'll find one of them first. + + Even if you don't get any answer, we think the experience of working on the problem, and then understanding the answer when we reveal it, will be satisfying and worthwhile. It also fits our pedagogical purposes for some of the recurring themes of the class. Numbers