Lists and List Comprehensions
In Kapulet, what does
[ [x, 2*x] | x from [1, 2, 3] ]evaluate to?
[ 10*x + y | y from , x from [1, 2, 3] ]evalaute to?
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, if you need it.
Last week you defined
headin terms of
fold_right. Your solution should be straightforwardly translatable into one that uses our proposed right-fold encoding of lists in the Lambda Calculus. Now define
empty?in the Lambda Calculus. (It should require only small modifications to your solution for
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
consbe, for this encoding?
Continuing to encode lists in terms of their left-folds, what should
last [a, b, c]should result in
last result in whatever
erris bound to.
Continuing to encode lists in terms of their left-folds, how should we write
head? This is challenging. Here is a solution, if you need help.
Suppose you have two lists of integers,
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 applying the built-in comparison operator
equal?to the lists themselves. (Nor on the operator
eqv?, which might not do what you expect.) 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, if you need it.
(The function you're trying to define here is like
eqlist?in Chapter 5 of The Little Schemer, though you are only concerned with lists of numbers, whereas the function from The Little Schemer also works on lists containing symbolic atoms --- and in the final version from that Chapter, also on lists that contain other, embedded lists.)
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
In last week's homework, you gave a Lambda Calculus definition of
succfor Church-encoded numbers. Can you now define
pred 0result in whatever
erris bound to. This is challenging. For some time theorists weren't sure it could be done. (Here is some interesting commentary.) However, in this week's notes we examined one strategy for defining
tailfor our chosen encodings of lists, and given the similarities we explained between lists and numbers, perhaps that will give you some guidance in defining
(Want a further challenge? Define
map2in the Lambda Calculus, using our right-fold encoding for lists, where
map2 g [a, b, c] [d, e, f]should evaluate to
[g a d, g b e, g c f]. Doing this will require drawing on a number of different tools we've developed, including that same strategy for defining
tail. Purely extra credit.)
leqfor numbers (that is, ≤) in the Lambda Calculus. Here is the expected behavior, where
succ zero, and
succ (succ zero).
leq zero zero ~~> true leq zero one ~~> true leq zero two ~~> true leq one zero ~~> false leq one one ~~> true leq one two ~~> true leq two zero ~~> false leq two one ~~> false leq two two ~~> true ...
You'll need to make use of the predecessor function, but it's not essential to understanding this problem that you have successfully implemented it yet. You can treat it as a black box.
Reduce the following forms, if possible:
- Give Combinatory Logic combinators (that is, expressed in terms of
I) that behave like our boolean functions. Provide combinators for
Using the mapping specified in this week's notes, translate the following lambda terms into combinatory logic:
\x y. x
\x y. y
\x y. y x
\x. x x
\x y z. x (y z)
For each of the above translations, how many
Is are there? Give a rule for describing what each
Icorresponds to in the original lambda term.
This generalization depends on you omitting the translation rule:
6. @a(Xa) = X if a is not in X
Evaluation strategies in Combinatory Logic
Find a term in CL that behaves like Omega does in the Lambda Calculus. Call it
Are there evaluation strategies in CL corresponding to leftmost reduction and rightmost reduction in the Lambda Calculus? What counts as a redex in CL?
Consider the CL term
K I Skomega. Does leftmost (alternatively, rightmost) evaluation give results similar to the behavior of
K I Omegain the Lambda Calculus, or different? What features of the Lambda Calculus and CL determine this answer?
What should count as a thunk in CL? What is the equivalent constraint in CL to forbidding evaluation inside of a lambda abstract?
More Lambda Practice
Reduce to beta-normal forms:
(\x. x (\y. y x)) (v w)
(\x. x (\x. y x)) (v w)
(\x. x (\y. y x)) (v x)
(\x. x (\y. y x)) (v y)
(\x y. x y y) u v
(\x y. y x) (u v) z w
(\x y. x) (\u u)
(\x y z. x z (y z)) (\u v. u)