

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CS 3721: Programming Languages Lab Lab #05 Solutions: eval expression
Due date: February 26, 3:30pm. At the beginning of the next recitation.
Goals of this lab:
More practice with recursion Learn how to use eval expression
(code-modifier '3 '7 '(* 3 (+ 3 5))) => (* 7 (+ 7 5)) (code-modifier '+ '* '(lambda (x) (+ x x))) => (lambda (x) (* x x)) Solution:
(define code-modifier (lambda (a b expr) (cond ((null? expr) '()) ; base case ((atom? expr) (if (eqv? a expr) b expr)) ; base case (else (let ((x (code-modifier a b (car expr))) (y (code-modifier a b (cdr expr)))) (cons x y))))))
(eval '(* 7 (+ 7 5))) => 84
(let ((f (eval '(lambda (g x) (g x x))))) (f * 5)) ; let body => 25 What do the following expressions evaluate to? Please, ll in the blanks. (eval '(- 7 12 5)) => -10 (eval (cons '- '(7 12 5))) => -10 (eval (cons '- (cdr '(7 12 5)))) => 7 Following expressions evaluate to 13, and 15, respectively. However, the expressions are not complete. Please, ll in the missing code. (eval (cons '+ (cons (eval '(+ 3 4)) (cons (eval '(* 2 3)) '() )))) => 13 (eval (cons '+ (cons (eval '(* 2 3)) (cons (eval '(+ 3 4)) '(2) )))) => 15 Now, using the eval expression, and your code-modi er function, write an expression that modi es the expression, (lambda (x y) (+ x y)) to (lambda (x y) (* x y)) and evaluates this modi ed function with the arguments 3, and 5. The result should be 15. (Hint: Think about building a convenient expression before passing it to the eval expression as an argument.) Solution:
(eval (cons (code-modifier '+ '* '(lambda (x y) (+ x y))) '(3 5)))