14 Functional Programming Exercises a. 5 ∗ 5 = 25 14.1 b. (( λy. 3 + y + z )2) = 3 + 2 + z = 5 + z c. ( λv. ( λw.w )( y ( λz.z ))) 14.2 The results are the same. In part b, where there are two beta reductions, lazy evaluation gives a different order: (( λx.x +2+ z )3) = 3+2+ z = 5+ z . 14.3 Evaluate the following expressions using your Scheme interpreter: (a) #t (b) #f (c) a (d) ((b c) d e) (e) (b c) (sum 1 2 3 4 5) = (+ 1 (sum 2 3 4 5)) 14.4 = (+ 1 (+ 2 (sum 3 4 5))) = (+ 1 (+ 2 (+ 3 (sum 4 5)))) = (+ 1 (+ 2 (+ 3 (+ 4 (sum 5))))) = (+ 1 (+ 2 (+ 3 (+ 4 (+ 5 (sum ()))))) = (+ 1 (+ 2 (+ 3 (+ 4 (+ 5 0))))) ... = 15 14.5 (define (elements lst) (if (null? lst) 0 (if (list? (car lst)) (+ (elements (car lst)) (elements (cdr lst))) (+ 1 (elements (cdr lst))) 63
64 14. FUNCTIONAL PROGRAMMING 14.6 (m-expression ’(plus (times (variable a) (variable b)) (value 2)) ’((a 2) (b 4))) = (+ (* (m-expression (variable a) ((a 2) (b 4))) (m-expression (variable b) ((a 2) (b 4)))) (value 2)) = ... = (+ (* 2 4) 2) = (+ 8 2) = 10 14.7 Add the following to the case before the else in applyBinary : ((lt) (< leftval rightval)) ((le) (<= leftval rightval)) ((eq) (= leftval rightval)) ((ne) (not (= leftval rightval))) ((gt) (> leftval rightval)) ((ge) (>= leftval rightval)) 14.8 (define (m-assignment statement state) (let ((target (car statement)) (source (cdr statement))) (onion target (m-expression source state) state) )) 14.9 Add the following case before the else in m-expression : ((not) (not (m-expression (cadr expr) state))) 14.10 Below is a sketch of the essentials of a static type checker for Clite. First, assume the abstract syntax of program and declarations to be: ; Program = (declarations block) ; Declarations = ((v1 t1) (v2 t2) ...) ; where vi = variable identifier ; ti = type, i.e., int, bool, float, or char) Because Declarations already has the structure of a Clite TypeMap , a separate typing function to create a TypeMap is unnecessary. Then it is only necessary to implement the typeOf function and the various validity functions V . For example: (define (v-program p) (let ((tm (car p)) (body (cadr p))) (and (v-decls tm) (v-stmt body tm)) )) (define (v-decls tm) (if (null? tm) #t (let ((d (caar tm)) (tail (cdr tm))) (if (isin? d tail) #f
65 (v-decls tail) )))) (define (isin? id lst) (if (null? lst) #f (if (equal? id (caar lst)) #t (isin? id (cdr lst)) ))) Here, e.g., is the validity check for assignment: (define (v-assign s tm) (let ((target (cadr s)) (source (caddr s))) (and (isin? target tm) (v-expr source tm) (equal? (get target tm) (typeof source tm)) ))) Note that because a state and a type map have the same structure, we can use our get function on a TypeMap to retrieve a type. Here are the beginnings of a function typeof : (define (typeof e tm) (let ((kind (car e)) (opnd (cadr e))) (case kind ((value) (boolean? opnd)) ((variable) (get opnd tm)) ((plus minus times div) (typeof opnd)) (else bool) ))) 14.11 Most of this is summarized in Section 14.2.8, and many of the missing pieces are included in earlier exercises. The challenge here is to complete the implementation of the Clite lnterpreter in Scheme and then demon- strate its completeness by interpreting a variety of abstract Clite programs. 14.12 Using a caret to denote exponentiation, add the following case to diff : ((^) (if (equals x u) (list * v (list ^ x (list - v 1))) (list * v (list * (list ^ u (list - v 1)) (diff x u))))) 14.13 Here are the results: a. > (diff ’x ’(+ (^ x 2) (+ (* 2 x) 1))) (+ (* 2 (^ x (- 2 1))) (+ (+ (* 2 1) (* x 0)) 0)) b. > (diff ’x ’(/ (- (* 5 x) (* 2 y)) (+ (^ x 2) 1))) (/ (- (* (+ (^ x 2) 1) (- (+ (* 5 1) (* x 0)) (+ (* 2 0) (* y 0)))) (* (- (* 5 x) (* 2 y)) (+ (* 2 (^ x (- 2 1))) 0))) (* (- (* 5 x) (* 2 y)) (+ (^ x 2) 1)))
66 14. FUNCTIONAL PROGRAMMING 14.14 The rules can be as extensive as desired; here will we consider only the two rules given as part of the problem, plus mutltiplfication by 0: a. x + 0 = 0 + x = x x ∗ 1 = 1 ∗ x = x x ∗ 0 = 0 ∗ x = 0 b. (define (simpl expr) (if (not (list? expr)) expr (let ((op (car expr)) (u ((simpl cadr expr))) (v ((simpl caddr expr)))) (case op ((+) (if (equal? u 0) v (if (equal? v 0) u (list ’+ u v)))) ((*) (if (or (equal? u 0) (equal? v 0)) 0 (if (equal? u 1) v (if (equal? v 1) u (list ’* u v))))) (else (list op u v) )))) c. c 1 + c 2 = c 3 where c 3 = c 1 + c 2 c 1 ∗ c 2 = c 4 where c 4 = c 1 ∗ c 2 d. (define (constArith expr) (if (not (list? expr)) expr (let ((u (constArith (cadr expr))) (v (constArith (caddr expr)))) (if (or (not (number? u)) (not (number? v))) expr (case (car expr) ((+) (+ u v)) ((*) (* u v)) (else (list (car expr) u v)) ))))) e. (define (simplify expr) (let ((se (constArith (simpl expr)))) (if (equal? expr se) expr (simplify se) ))) a. No solution prints. 14.15 b. The solution is: (4 2 5 3 1) c. The solution is: (5 3 1 6 4 2). Backtracking is required.
67 14.16 The Knights tour solution is in the file knight.scm in the ch14code directory. The function try expects the initial knight position to be given, e.g.: (try ((3 3))) The board is kept in reverse order; the current position of the knight is at the front of the list. Each knight position is given as a list of row, column entries; in the example above, the knight is at row 3, column 3. A move is kept as an integer from 1 to the length of trial, which is a list of knight moves relative to its current position. This implementation required a slight modification to the function trywh . 14.17 14.18 14.19 Evaluate the following expressions using your Haskell interpreter: a. 3 b. Error: index (5) too large. c. 1 d. [2,3,4,5] 14.20 a. [(”Jane”,1223345),(”Bob”,2771234),(”Allen”,2772345),(”Bob”,2770123)] b. [(”Allen”,2772345),(”Bob”,2770123)] 14.21 deleteEntry :: Person -> Phonebook -> Phonebook deleteEntry p pb = [entry | entry <- pb, fst(entry) /= p] 14.22 elements [] = 0 elements (x:xs) = 1 + elements(xs) 14.23 The file Clite.hs in the ch14code directory is the skeleton of a Clite interpreter in Haskell. It contains the definitions in Section 14.3.8 as well as the following function. m (Block b) state | b == [] = state | otherwise = m (Block (tail b)) (m (head b) state) 14.24 The file Clite.hs in the ch14code directory is the skeleton of a Clite interpreter in Haskell (including int and bool values, but not char and float). It contains the following expansion of the eval function on page 403, along with a new apply function:
68 14. FUNCTIONAL PROGRAMMING eval :: Expression -> State -> Value eval (Var v) state = get v state eval (Lit v) state = v eval (Binary op e1 e2) state = apply op (eval e1 state) (eval e2 state) apply :: Op -> Value -> Value -> Value apply "+" (Intval a) (Intval b) = (Intval (a + b)) apply "-" (Intval a) (Intval b) = (Intval (a - b)) apply "*" (Intval a) (Intval b) = (Intval (a * b)) apply "/" (Intval a) (Intval b) = (Intval (a ‘div‘ b)) apply "<" (Intval a) (Intval b) = (Boolval (a < b)) apply "<=" (Intval a) (Intval b) = (Boolval (a <= b)) apply "==" (Intval a) (Intval b) = (Boolval (a == b)) apply "!=" (Intval a) (Intval b) = (Boolval (a /= b)) apply ">=" (Intval a) (Intval b) = (Boolval (a >= b)) apply ">" (Intval a) (Intval b) = (Boolval (a > b)) apply "&&" (Boolval a) (Boolval b) = (Boolval (a && b)) apply "||" (Boolval a) (Boolval b) = (Boolval (a || b)) 14.25 The file Clite.hs in the ch14code directory contains the following ex- panded definition of Expression given on page 403, along with expanded definitions of eval and apply given above, and the new function applyUnary : data Expression = ... | Unary Op Expression deriving (Eq, Ord, Show) eval (Unary op e) state = applyUnary op (eval e state) applyUnary :: Op -> Value -> Value applyUnary "!" (Boolval a) = (Boolval (not a)) 14.26 Here is a summary: Let s = [("x", (Intval 5)), ("y", (Intval 3)), ("z", (Intval 1))] Then eval (Binary "+" (Var "y") (Lit (Intval 2))) s = apply "+" (eval (Var "y") s) (eval (Lit (Intval 2)) s) = apply "+" (get "y" s) (eval (Lit (Intval 2)) s) = apply "+" (Intval 3) (Intval 2) = Intval 3+2 = Intval 5 14.27 length [] = 0 length (w:ws) = 1 + length ws a. fibSlow(25) delivers the result 75025 in about 20 seconds. fibSlow(50) 14.28 may still be running as you read this solution! The reason for this
Recommend
More recommend