Question 1 @ 2025-03-18 18:26
Consider the following optimization from the class notes:
(define (foo list)
(map (lambda (n) (if E0 E1 E2))
list))
;; -->
(define (foo list)
(map (if E0
(lambda (n) E1)
(lambda (n) E2))
list))
(map (lambda (n) (if E0 E1 E2))
list))
;; -->
(define (foo list)
(map (if E0
(lambda (n) E1)
(lambda (n) E2))
list))
What is an expression examples for E0
for which this would be a valid
transformation?
- This transformation is always valid
-
Impossible to answer without knowing what are
E1
andE2
-
(if (< n 10) (add1 n) (sub1 n))
-
(if (< (length list) 3) (add1 n) (sub1 n))
- Trick question: the code is broken anyway
Question 2 @ 2025-03-18 18:28
Still considering the same optimization:
(define (foo list)
(map (lambda (n) (if E0 E1 E2))
list))
;; -->
(define (foo list)
(map (if E0
(lambda (n) E1)
(lambda (n) E2))
list))
(map (lambda (n) (if E0 E1 E2))
list))
;; -->
(define (foo list)
(map (if E0
(lambda (n) E1)
(lambda (n) E2))
list))
What are the required conditions to make this a good transformation?
- The language must be lazy
- The language must be eager
- The language must be pure (no side effects)
-
The
E0
expression must not have any side effects -
The
E0
expression must not depend onn
-
The
E0
expression must be syntactically correct
Question 3 @ 2025-03-18 18:31
We’ve talked about the Toy language that we’re now using. Consider the following code in this language:
{bind {{+ *}
{* +}}
{* {+ 1 10} 100}}
{* +}}
{* {+ 1 10} 100}}
What would this evaluate to?
- 110
- 111
- 1000
- 1100
- Syntax error
- Runtime error
Question 4 @ 2025-03-18 18:33
Continuing with our new toy — the Toy language — extended with a
set!
and multi-body-expressions as done in the HW. What would the following produce?
{bind {{* *}}
{set! + -}
{set! * -}
{* 100 {+ 10 1}}}
{set! + -}
{set! * -}
{* 100 {+ 10 1}}}
What would this evaluate to?
- 89
- 91
- 110
- 900
- 1100
- Syntax error
- Runtime error
Question 5 @ 2025-03-18 18:36
Suppose we run more code after we ran the previous example:
(run "{bind {{* *}}
{set! + -}
{set! * -}
{* 100 {+ 10 1}}}")
(list (run "{+ 10 2}") (run "{* 10 2}"))
{set! + -}
{set! * -}
{* 100 {+ 10 1}}}")
(list (run "{+ 10 2}") (run "{* 10 2}"))
What would the above return?
-
(8 12)
-
(8 20)
-
(12 20)
-
(12 12)
-
Trick question: we cannot
run
stuff more than once