From: jim Date: Sat, 14 Feb 2015 12:50:33 +0000 (-0500) Subject: add list_equal? and zero?, formatting X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=c8cafa30e8f5eb875eef4003e927df51c62ebc09 add list_equal? and zero?, formatting --- diff --git a/exercises/assignment3.mdwn b/exercises/assignment3.mdwn index 95450217..9dfece9a 100644 --- a/exercises/assignment3.mdwn +++ b/exercises/assignment3.mdwn @@ -1,6 +1,6 @@ ** *Work In Progress* ** -## Comprehensions +## Lists and List Comprehensions 1. In Kapulet, what does `[ [x, 2*x] | x from [1, 2, 3] ]` evaluate to? @@ -8,9 +8,6 @@ 3. Using either Kapulet's or Haskell's list comprehension syntax, write an expression that transforms `[3, 1, 0, 2]` into `[3, 3, 3, 1, 2, 2]`. [[Here is a hint|assignment3 hint1]], if you need it. - -## Lists - 4. Last week you defined `head` in the Lambda Calculus, using our proposed encoding for lists. Now define `empty?` (It should require only small modifications to your solution for `head`.) 5. If we encode lists in terms of their *left*-folds, instead, `[a, b, c]` would be encoded as `\f z. f (f (f z a) b) c`. The empty list `[]` would still be encoded as `\f z. z`. What should `cons` be, for this encoding? @@ -19,12 +16,16 @@ 7. Continuing to encode lists in terms of their left-folds, how should we write `head`? This is challenging. [[Here is a solution|assignment3 hint2]], if you need help. +8. Suppose you have two lists of integers, `left` and `right`. You want to determine whether those lists are equal, that is, whether they have all the same members in the same order. How would you implement such a list comparison? You can write it in Scheme or Kapulet using `letrec`, or if you want more of a challenge, in the Lambda Calculus using your preferred encoding for lists. If you write it in Scheme, don't rely on the built-in comparison operator `equal?` (nor on the operator `eqv?`, which won't do what you expect with lists). You can however rely on the comparison operator `=` which accepts only number arguments. If you write it in the Lambda Calculus, you can use your implementation of `leq`, requested below, to write an equality operator for Church-encoded numbers. [[Here is a hint|assignment3 hint3]], if you need it. + ## Numbers -8. Recall our proposed encoding for the numbers, called "Church's encoding". As we explained last week, it's similar to our proposed encoding of lists in terms of their folds. In last week's homework, you defined `succ` for numbers so encoded. Can you now define `pred` in the Lambca Calculus? Let `pred 0` result in whatever `err` is bound to. This is challenging. For some time theorists weren't sure it could be done. (Here is [some interesting commentary](http://okmij.org/ftp/Computation/lambda-calc.html#predecessor).) However, in this week's notes we examined one strategy for defining `tail` for our chosen encodings of lists, and given the similarities we explained between lists and numbers, perhaps that will give you some guidance in defining `pred` for numbers. +9. Recall our proposed encoding for the numbers, called "Church's encoding". As we explained last week, it's similar to our proposed encoding of lists in terms of their folds. Give a Lambda Calculus definition of `zero?` for numbers so encoded. (It should require only small modifications to your solution for `empty?`, above.) + +10. In last week's homework, you gave a Lambda Calculus definition of `succ` for Church-encoded numbers. Can you now define `pred`? Let `pred 0` result in whatever `err` is bound to. This is challenging. For some time theorists weren't sure it could be done. (Here is [some interesting commentary](http://okmij.org/ftp/Computation/lambda-calc.html#predecessor).) However, in this week's notes we examined one strategy for defining `tail` for our chosen encodings of lists, and given the similarities we explained between lists and numbers, perhaps that will give you some guidance in defining `pred` for numbers. -9. Define `leq` for numbers (that is, ≤) in the Lambda Calculus. Here is the expected behavior, +11. Define `leq` for numbers (that is, ≤) in the Lambda Calculus. Here is the expected behavior, where `one` abbreviates `succ zero`, and `two` abbreviates `succ (succ zero)`. leq zero zero ~~> true @@ -44,46 +45,50 @@ where `one` abbreviates `succ zero`, and `two` abbreviates `succ (succ zero)`. Reduce the following forms, if possible: -10. `Kxy` -11. `KKxy` -12. `KKKxy` -13. `SKKxy` -14. `SIII` -15. `SII(SII)` +
+
1. `Kxy` +
2. `KKxy` +
3. `KKKxy` +
4. `SKKxy` +
5. `SIII` +
6. `SII(SII)` +
-16. Give Combinatory Logic combinators (that is, expressed in terms of `S`, `K`, and `I`) that behave like our boolean functions. You'll need combinators for `true`, `false`, `neg`, `and`, `or`, and `xor`. +18. Give Combinatory Logic combinators (that is, expressed in terms of `S`, `K`, and `I`) that behave like our boolean functions. Provide combinators for `true`, `false`, `neg`, `and`, `or`, and `xor`. Using the mapping specified in this week's notes, translate the following lambda terms into combinatory logic: -17. `\x x` -18. `\x y. x` -19. `\x y. y` -20. `\x y. y x` -21. `\x. x x` -22. `\x y z. x (y z)` +
+
1. `\x x` +
2. `\x y. x` +
3. `\x y. y` +
4. `\x y. y x` +
5. `\x. x x` +
6. `\x y z. x (y z)` +
-23. For each of the above translations, how many `I`s are there? Give a rule for describing what each `I` corresponds to in the original lambda term. +25. For each of the above translations, how many `I`s are there? Give a rule for describing what each `I` corresponds to in the original lambda term. Evaluation strategies in Combinatory Logic ------------------------------------------ -23. Find a term in CL that behaves like Omega does in the Lambda -Calculus. Call it Skomega. +26. Find a term in CL that behaves like Omega does in the Lambda +Calculus. Call it `Skomega`. -24. Are there evaluation strategies in CL corresponding to leftmost -reduction and rightmost reduction in the lambda calculus? +27. Are there evaluation strategies in CL corresponding to leftmost +reduction and rightmost reduction in the Lambda Calculus? What counts as a redex in CL? -25. Consider the CL term K I Skomega. Does leftmost (alternatively, -rightmost) evaluation give results similar to the behavior of K I -Omega in the lambda calculus, or different? What features of the -lambda calculus and CL determine this answer? +28. Consider the CL term `K I Skomega`. Does leftmost (alternatively, +rightmost) evaluation give results similar to the behavior of `K I +Omega` in the Lambda Calculus, or different? What features of the +Lambda Calculus and CL determine this answer? -26. What should count as a thunk in CL? What is the equivalent +29. What should count as a thunk in CL? What is the equivalent constraint in CL to forbidding evaluation inside of a lambda abstract? @@ -92,15 +97,15 @@ More Lambda Practice Reduce to beta-normal forms: -
-
1. `(\x. x (\y. y x)) (v w)` -
2. `(\x. x (\x. y x)) (v w)` -
3. `(\x. x (\y. y x)) (v x)` -
4. `(\x. x (\y. y x)) (v y)` +
+
1. `(\x. x (\y. y x)) (v w)` +
2. `(\x. x (\x. y x)) (v w)` +
3. `(\x. x (\y. y x)) (v x)` +
4. `(\x. x (\y. y x)) (v y)` -
5. `(\x y. x y y) u v` -
6. `(\x y. y x) (u v) z w` -
7. `(\x y. x) (\u u)` -
8. `(\x y z. x z (y z)) (\u v. u)` +
9. `(\x y. x y y) u v` +
10. `(\x y. y x) (u v) z w` +
11. `(\x y. x) (\u u)` +
12. `(\x y z. x z (y z)) (\u v. u)`