X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=assignment1.mdwn;h=ea8f2511992e97ff0caed2f8f002426dd2bc053b;hb=4c7835b181efbb99f16560e6f48aa935480beef8;hp=05787ca26c30275ada68663beef21c9b98bb8228;hpb=4288468c7b4a4d0bb6ec7eab2c1d71803716c726;p=lambda.git
diff --git a/assignment1.mdwn b/assignment1.mdwn
index 05787ca2..ea8f2511 100644
--- a/assignment1.mdwn
+++ b/assignment1.mdwn
@@ -1,158 +1,118 @@
-Reduction
----------
+1. Define a function `zero?` that expects a single number as an argument, and returns `'true` if that number is `0`, else returns `'false`. Your solution should have a form something like this:
-Find "normal forms" for the following---that is, reduce them until no more reductions are possible. We'll write λx
as `\x`.
+ let
+ zero? match lambda x. FILL_IN_THIS_PART
+ in zero?
-1. `(\x \y. y x) z`
-2. `(\x (x x)) z`
-3. `(\x (\x x)) z`
-4. `(\x (\z x)) z`
-5. `(\x (x (\y y))) (\z (z z))`
-6. `(\x (x x)) (\x (x x))`
-7. `(\x (x x x)) (\x (x x x))`
+ You can use the `if...then...else` construction if you like, but it will make it easier to generalize to later problems if you use the `case EXPRESSION of PATTERN1 then RESULT1; PATTERN2 then RESULT2; ... end` construction instead.
+2. Define a function `empty?` that expects a sequence of values as an argument (doesn't matter what type of values), and returns `'true` if that sequence is the empty sequence `[]`, else returns `'false`. Here your solution should have a form something like this:
-Booleans
---------
+ let
+ empty? match lambda xs. case xs of
+ FILL_IN_THIS_PART
+ end
+ in empty?
-Recall our definitions of true and false.
+3. Define a function `tail` that expects a sequence of values as an argument (doesn't matter what type of values), and returns that sequence with the first element (if any) stripped away. (Applying `tail` to the empty sequence `[]` can just give us back the empty sequence.)
-> `true` defined to be `\t \f. t`
-> `false` defined to be `\t \f. f`
+4. Define a function `drop` that expects two arguments, in the form (*number*, *sequence*), and works like this:
-In Racket, these can be defined like this:
+ drop (0, [10, 20, 30]) # evaluates to [10, 20, 30]
+ drop (1, [10, 20, 30]) # evaluates to [20, 30]
+ drop (2, [10, 20, 30]) # evaluates to [30]
+ drop (3, [10, 20, 30]) # evaluates to []
+ drop (4, [10, 20, 30]) # evaluates to []
- (define true (lambda (t) (lambda (f) t)))
- (define false (lambda (t) (lambda (f) f)))
+ Your solution should have a form something like this:
-
-- Define a `neg` operator that negates `true` and `false`.
+ letrec
+ drop match lambda (n, xs). FILL_IN_THIS_PART
+ in drop
-Expected behavior:
+ What is the relation between `tail` and `drop`?
- (((neg true) 10) 20)
+5. Define a function `take` that expects two arguments, in the same form as `drop`, but works like this instead:
-evaluates to 20, and
+ take (0, [10, 20, 30]) # evaluates to []
+ take (1, [10, 20, 30]) # evaluates to [10]
+ take (2, [10, 20, 30]) # evaluates to [10, 20]
+ take (3, [10, 20, 30]) # evaluates to [10, 20, 30]
+ take (4, [10, 20, 30]) # evaluates to [10, 20, 30]
- (((neg false) 10) 20)
+6. Define a function `split` that expects two arguments, in the same form as `drop` and `take`, but this time evaluates to a pair of results. It works like this:
-evaluates to 10.
+ split (0, [10, 20, 30]) # evaluates to ([], [10, 20, 30])
+ split (1, [10, 20, 30]) # evaluates to ([10], [20, 30])
+ split (2, [10, 20, 30]) # evaluates to ([10, 20], [30])
+ split (3, [10, 20, 30]) # evaluates to ([10, 20, 30], [])
+ split (4, [10, 20, 30]) # evaluates to ([10, 20, 30], [])
-
- Define an `and` operator.
+ Here's a way to answer this problem making use of your answers to previous questions:
-
- Define an `xor` operator.
+ letrec
+ drop match ... ; # as in problem 4
+ take match ... ; # as in problem 5
+ split match lambda (n, xs). let
+ ys = take (n, xs);
+ zs = drop (n, xs)
+ in (ys, zs)
+ in split
-If you haven't seen this term before, here's a truth table:
+ However, we want you to instead write this function from scratch.
- true xor true = false
- true xor false = true
- false xor true = true
- false xor false = false
+7. Write a function `filter` that expects two arguments. The second argument will be a sequence `xs` with elements of some type *t*, for example numbers. The first argument will be a function `p` that itself expects arguments of type *t* and returns `'true` or `'false`. What `filter` should return is a sequence that contains exactly those members of `xs` for which `p` returned `'true`. For example, helping ourself to a function `odd?` that works as you'd expect:
+ filter (odd?, [11, 12, 13, 14]) # evaluates to [11, 13]
+ filter (odd?, [11]) # evaluates to [11]
+ filter (odd?, [12, 14]) # evaluates to []
-
- Inspired by our definition of boolean values, propose a data structure
-capable of representing one of the two values `black` or `white`.
-If we have
-one of those values, call it a "black-or-white value", we should be able to
-write:
+8. Write a function `partition` that expects two arguments, in the same form as `filter`, but this time evaluates to a pair of results. It works like this:
- the-value if-black if-white
+ partition (odd?, [11, 12, 13, 14]) # evaluates to ([11, 13], [12, 14])
+ partition (odd?, [11]) # evaluates to ([11], [])
+ partition (odd?, [12, 14]) # evaluates to ([], [12, 14])
-(where `if-black` and `if-white` are anything), and get back one of `if-black` or
-`if-white`, depending on which of the black-or-white values we started with. Give
-a definition for each of `black` and `white`. (Do it in both lambda calculus
-and also in Racket.)
+9. Write a function `double` that expects one argument which is a sequence of numbers, and returns a sequence of the same length with the corresponding elements each being twice the value of the original element. For example:
-
- Now propose a data structure capable of representing one of the three values
-`red` `green` or `blue`, based on the same model. (Do it in both lambda
-calculus and also in Racket.)
-
+ double [10, 20, 30] # evaluates to [20, 40, 60]
+ double [] # evaluates to []
+10. Write a function `map` that generalizes `double`. This function expects a pair of arguments, the second being a sequence `xs` with elements of some type *t*, for example numbers. The first argument will be a function `f` that itself expects arguments of type *t* and returns some type *t'* of result. What `map` should return is a sequence of the results, in the same order as the corresponding original elements. The result should be that we could say:
+ letrec
+ map match lambda (f, xs). FILL_IN_THIS_PART;
+ double match lambda xs. map ((lambda x. 2*x), xs)
+ in ...
-Pairs
------
+11. Write a function `map2` that generalizes `map`. This function expects a triple of arguments: the first being a function `f` as for `map`, and the second and third being two sequences. In this case `f` is a function that expects *two* arguments, one from the first of the sequences and the other from the corresponding position in the other sequence. The result should behave like this:
-Recall our definitions of ordered pairs.
+ map2 ((lambda (x,y). 10*x + y), [1, 2, 3], [4, 5, 6]) # evaluates to [14, 25, 36]
-> the pair (x,y) is defined as `\f. f x y`
-To extract the first element of a pair p, you write:
+###Extra credit problems###
- p (\fst \snd. fst)
+* In class I mentioned a function `&&` which occupied the position *between* its arguments, rather than coming before them (this is called an "infix" function). The way that it works is that `[1, 2, 3] && [4, 5]` evaluates to `[1, 2, 3, 4, 5]`. Define this function, making use of `letrec` and the simpler infix operation `&`.
-Here are some definitions in Racket:
+* Write a function `unmap2` that is something like the inverse of `map2`. This function expects two arguments, the second being a sequence of elements of some type *t*. The first is a function `g` that expects a single argument of type *t* and returns a *pair* of results, rather than just one result. We want to collate these results, the first into one list, and the second into a different list. Then `unmap2` should return those two lists. Thus if:
- (define make-pair (lambda (fst) (lambda (snd) (lambda (f) ((f fst) snd)))))
- (define get-first (lambda (fst) (lambda (snd) fst)))
- (define get-second (lambda (fst) (lambda (snd) snd)))
+ g z1 # evaluates to [x1, y1]
+ g z2 # evaluates to [x2, y2]
+ g z3 # evaluates to [x3, y3]
-Now we can write:
+ Then `unmap2 (g, [z1, z2, z3])` should evaluate to `([x1, x2, x3], [y1, y2, y3])`.
- (define p ((make-pair 10) 20))
- (p get-first) ; will evaluate to 10
- (p get-second) ; will evaluate to 20
+* Write a function `takewhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this:
-If you're bothered by having the pair 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 elemens with that
-function. In other words, the pair is also a function. (Of course, in the
-untyped lambda calculus, absolutely *everything* is a function: functors,
-arguments, abstracts, redexes, values---everything.)
+ takewhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [1, 2]
-If you like, you can disguise what's going on like this:
+ Note that we stop "taking" once we reach `20`, even though there are still later elements in the list that are less than `10`.
- (define lifted-get-first (lambda (p) (p get-first)))
- (define lifted-get-second (lambda (p) (p get-second)))
+* Write a function `dropwhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this:
-Now you can write:
+ dropwhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [20, 4, 40]
- (lifted-get-first p)
+ Note that we stop "dropping" once we reach `20`, even though there are still later elements in the list that are less than `10`.
-instead of:
-
- (p get-first)
-
-However, the latter is still what's going on under the hood.
-
-
-
-- Define a `swap` function that reverses the elements of a pair.
-
-Expected behavior:
-
- (define p ((make-pair 10) 20))
- ((p swap) get-first) ; evaluates to 20
- ((p swap) get-second) ; evaluates to 10
-
-Write out the definition of swap in Racket.
-
-
-
- Define a `dup` function that duplicates its argument to form a pair
-whose elements are the same.
-Expected behavior:
-
- ((dup 10) get-first) ; evaluates to 10
- ((dup 10) get-second) ; evaluates to 10
-
-
- Define a `sixteen` function that makes
-sixteen copies of its argument (and stores them in a data structure of
-your choice).
-
-
- Inspired by our definition of ordered pairs, propose a data structure capable of representing ordered triples. That is,
-
- (((make-triple M) N) P)
-
-should return an object that behaves in a reasonable way to serve as a triple. In addition to defining the `make-triple` function, you have to show how to extract elements of your triple. Write a `get-first-of-triple` function, that does for triples what `get-first` does for pairs. Also write `get-second-of-triple` and `get-third-of-triple` functions.
-
-
- Write a function `second-plus-third` that when given to your triple, returns the result of adding the second and third members of the triple.
-
-You can help yourself to the following definition:
-
- (define add (lambda (x) (lambda (y) (+ x y))))
-
-
-
-
+* Write a function `reverse` that returns the reverse of a sequence. Thus, `reverse [1, 2, 3, 4]` should evaluate to `[4, 3, 2, 1]`.