X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=assignment_3_evaluator.mdwn;h=aba94f89cd0a21404f6e7fc51c119cd24e0baaba;hp=28693f436d9360d5603354a48ecf498e32633e8f;hb=73b21c99d5a3f8184681a1fa1e0bb2b44ab4c34f;hpb=4cea4f69242f3a229292186c8fa652942f31f8b8 diff --git a/assignment_3_evaluator.mdwn b/assignment_3_evaluator.mdwn index 28693f43..aba94f89 100644 --- a/assignment_3_evaluator.mdwn +++ b/assignment_3_evaluator.mdwn @@ -22,6 +22,7 @@ let mylist = make\_list 1 (make\_list 2 (make\_list 3 empty)) in ; church numerals let iszero = \n. n (\x. false) true in let succ = \n s z. s (n s z) in +let add = \l r. l succ r in let mul = \m n s. m (n s) in let pred = (\shift n. n shift (make\_pair 0 0) get\_snd) (\p. p (\x y. make\_pair (succ x) x)) in let leq = \m n. iszero(n pred m) in @@ -29,8 +30,8 @@ let eq = \m n. and (leq m n)(leq n m) in ; ; a fixed-point combinator for defining recursive functions let Y = \f. (\h. f (h h)) (\h. f (h h)) in -; let length = Y (\length l. isempty l 0 (succ (length (tail l)))) in +let fold = Y (\f l g z. isempty l z (g (head l)(f (tail l) g z))) in ; ; synonyms let makePair = make\_pair in @@ -42,24 +43,27 @@ let makeList = make\_list in let isZero = iszero in let mult = mul in ; -let t1 = (make_list 1 empty) in -let t2 = (make_list 2 empty) in -let t3 = (make_list 3 empty) in -let t12 = (make_list t1 (make_list t2 empty)) in -let t23 = (make_list t2 (make_list t3 empty)) in -let ta = (make_list t1 t23) in -let tb = (make_list t12 t3) in -let tc = (make_list t1 (make_list t23 empty)) in +let t1 = (make\_list 1 empty) in +let t2 = (make\_list 2 empty) in +let t3 = (make\_list 3 empty) in +let t12 = (make\_list t1 (make\_list t2 empty)) in +let t23 = (make\_list t2 (make\_list t3 empty)) in +let ta = (make\_list t1 t23) in +let tb = (make\_list t12 (make\_list t3 empty)) in +let tc = (make\_list t1 (make\_list t23 empty)) in ; -;sum-leaves t1 ~~> 1 -;sum-leaves t2 ~~> 2 -;sum-leaves t3 ~~> 3 -;sum-leaves t12 ~~> 3 -;sum-leaves t23 ~~> 5 -;sum-leaves ta ~~> 6 -;sum-leaves tb ~~> 6 +;sum-leaves t1 ; ~~> 1 +;sum-leaves t2 ; ~~> 2 +;sum-leaves t3 ; ~~> 3 +;sum-leaves t12 ; ~~> 3 +;sum-leaves t23 ; ~~> 5 +;sum-leaves ta ; ~~> 6 +;sum-leaves tb ; ~~> 6 +;sum-leaves tc ; ~~> 6 ; -length (tail mylist) +; updated: added add, and fold for v1 lists; and defn of tb fixed +; hint: +fold mylist add 0