From 7cc39c68d74503b3ea9d143ebe9acf21ff425042 Mon Sep 17 00:00:00 2001 From: Jim Date: Sat, 7 Feb 2015 20:18:22 -0500 Subject: [PATCH] push assignment2 --- content.mdwn | 2 +- .../assignment2.mdwn | 0 exercises/assignment2_hint.mdwn | 30 ++++++++++++++++++++++ index.mdwn | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) rename topics/_assignment2.mdwn => exercises/assignment2.mdwn (100%) create mode 100644 exercises/assignment2_hint.mdwn diff --git a/content.mdwn b/content.mdwn index 4f9ae9f1..e4e81495 100644 --- a/content.mdwn +++ b/content.mdwn @@ -26,7 +26,7 @@ Week 2: * [[Introduction to the Lambda Calculus|topics/week2 lambda intro]] * [[Advanced notes on the Lambda Calculus|topics/week2 lambda advanced]] * [[Encoding Booleans, Tuples, Lists, and Numbers|topics/week2 encodings]]; -* Homework for week 2 (in progress) +* [[Homework|exercises/assignment2]] diff --git a/topics/_assignment2.mdwn b/exercises/assignment2.mdwn similarity index 100% rename from topics/_assignment2.mdwn rename to exercises/assignment2.mdwn diff --git a/exercises/assignment2_hint.mdwn b/exercises/assignment2_hint.mdwn new file mode 100644 index 00000000..3a83714f --- /dev/null +++ b/exercises/assignment2_hint.mdwn @@ -0,0 +1,30 @@ +The problem was: + +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. + +### Here is a 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? + +5. So now put it all together, and explain how to express `reverse xs` using `fold_right` and primitive syntax like `lambda ...`, `&`, and `[]`... without using `letrec`! + diff --git a/index.mdwn b/index.mdwn index b043fd81..2e6bdb64 100644 --- a/index.mdwn +++ b/index.mdwn @@ -94,7 +94,7 @@ The [[differences between our made-up language and Scheme, OCaml, and Haskell|ro [[Intro to the Lambda Calculus|topics/week2 lambda intro]]; [[Advanced notes|topics/week2 lambda advanced]]; [[Encoding Booleans, Tuples, Lists, and Numbers|topics/week2 encodings]]; -Homework (in progress) +[[Homework|exercises/assignment2]] > Also, if you're reading the Hankin book, try reading Chapters 1-3. You will most likely need to come back again and read it multiple times; but this would be a good time to make the first attempt. -- 2.11.0