4 Once again, the lambda evaluator will make working through this
5 assignment much faster and more secure.
7 #Writing recursive functions on version 1 style lists#
9 Recall that version 1 style lists are constructed like this (see
10 [[lists and numbers]]):
15 let false = \x y. y in
16 let and = \l r. l (r true false) false in
19 let makePair = \f s g. g f s in
22 let nil = makePair true meh in
23 let isNil = \x. x fst in
24 let makeList = \h t. makePair false (makePair h t) in
25 let head = \l. isNil l err (l snd fst) in
26 let tail = \l. isNil l err (l snd snd) in
28 ; a list of numbers to experiment on
29 let mylist = makeList 1 (makeList 2 (makeList 3 nil)) in
31 ; a fixed-point combinator for defining recursive functions
32 let Y = \f. (\h. f (h h)) (\h. f (h h)) in
35 let isZero = \n. n (\x. false) true in
36 let succ = \n s z. s (n s z) in
37 let mult = \m n s. m (n s) in
38 let length = Y (\length l. isNil l 0 (succ (length (tail l)))) in
39 let pred = \n. isZero n 0 (length (tail (n (\p. makeList meh p) nil)))
41 let leq = \m n. isZero(n pred m) in
42 let eq = \m n. and (leq m n)(leq n m) in
48 Then `length mylist` evaluates to 3.
50 1. What does `head (tail (tail mylist))` evaluate to?
52 2. Using the `length` function as a model, and using the predecessor
53 function, write a function that computes factorials. (Recall that n!,
54 the factorial of n, is n times the factorial of n-1.)
56 Warning: my browser isn't able to compute factorials of numbers
57 greater than 2 (it does't provide enough resources for the JavaScript
58 interpreter; web pages are not supposed to be that computationally
61 3. (Easy) Write a function `listLenEq` that returns true just in case
65 listLenEq mylist (makeList meh (makeList meh (makeList meh nil)))
68 listLenEq mylist (makeList meh (makeList meh nil))) ~~> false
71 4. (Still easy) Now write the same function, but don't use the length
74 5. In assignment 2, we discovered that version 3-type lists (the ones
76 work like Church numerals) made it much easier to define operations
77 like `map` and `filter`. But now that we have recursion in our
79 reasonable map and filter functions for version 1 lists are within our
80 reach. Give definitions for `map` and a `filter` for verson 1 type
83 #Computing with trees#
85 Linguists analyze natural language expressions into trees.
86 We'll need trees in future weeks, and tree structures provide good
87 opportunities for learning how to write recursive functions.
88 Making use of the resources we have at the moment, we can approximate
89 trees as follows: instead of words, we'll use Church numerals.
90 Then a tree will be a (version 1 type) list in which each element is
91 itself a tree. For simplicity, we'll adopt the convention that
92 a tree of length 1 must contain a number as its only element.
93 Then we have the following representations:
102 [[1];[2];[3]] [[[1];[2]];[3]] [[1];[[2];[3]]]
105 Limitations of this scheme include the following: there is no easy way
106 to label a constituent with a syntactic category (S or NP or VP,
107 etc.), and there is no way to represent a tree in which a mother has a
110 When processing a tree, you can test for whether the tree contains
111 only a numeral (in which case the tree is leaf node) by testing for
112 whether the length of the list is less than or equal to 1. This will
113 be your base case for your recursive functions that operate on these
116 1. Write a function that sums the number of leaves in a tree.
121 let t1 = (makeList 1 nil) in
122 let t2 = (makeList 2 nil) in
123 let t3 = (makeList 3 nil) in
124 let t12 = (makeList t1 (makeList t2 nil)) in
125 let t23 = (makeList t2 (makeList t3 nil)) in
126 let ta = (makeList t1 t23) in
127 let tb = (makeList t12 t3) in
128 let tc = (makeList t1 (makeList t23 nil)) in
140 2. Write a function that counts the number of leaves.