For each closure created while evaluating each of the following programs, indicate the contents of the environment for that closure. Use a format like
(lambda (x) ...) has env (a = 1) (b = 9) etc.
((lambda (x y)
((lambda ()
((lambda (x)
(lambda (v) (* v x y)))
x))))
9 1)
;; more-fans : symbol symbol list[symbol] -> symbol ;; determine which team listed more times in input list (define (more-fans team1 team2 alot) (cond [(> (length (filter (lambda (teamname) (symbol=? team1 teamname)) alot)) (length (filter (lambda (teamname) (symbol=? team2 teamname)) alot))) team1] [else team2]))
;; transform-circle : circle list[circle -> circle] -> circle
;; applies each function in list to a circle in order and returns final circle
(define (transform-circle acirc alot)
(cond [(empty? alot) acirc]
[(cons? alot)
(transform-circle ((first alot) acirc) (rest alot))]))
;; make-scaler-func : num -> (circle -> circle)
;; creates function that scales a circle by the given factor
(define (make-scaler-func factor)
(lambda (acirc)
(make-circle (circle-center acirc) (* factor (circle-radius acirc)))))
;; make-mover-func : num num -> (circle -> circle)
;; creates function that moves a circle by the given coordinate changes
(define (make-mover-func delta-x delta-y)
(lambda (acirc)
(make-circle (make-posn (+ delta-x (posn-x (circle-center acirc)))
(+ delta-y (posn-y (circle-center acirc))))
(circle-radius acirc))))
(transform-circle (make-circle (make-posn 10 40) 50)
(list (make-scaler-func 5)
(make-mover-func -20 30)
(make-scaler-func 12)))
For the following program, write down the environments that are in use when each of the following (sub-)expressions gets evaluated. If an expression is evaluated more than once, write down the environments that are in use at each evaluation (in the order that the evaluations occur).
((lambda (alon)
((lambda (f y)
((lambda (num alon)
(map (lambda (num) (+ (f num) y))
alon))
8 (list y (* y 2) (* y 4))))
(lambda (num) (+ num 5))
(first alon)))
(list 1 3 5 7))
Convert each of the following programs such that all calls to scripts are in script-position.
(define (fscript x) (+ 3 (* 2 x))) (define (gscript y z) (+ (* y y) (* z z))) (fscript (gscript (fscript 7) 8))
(define (square-script x) (* x x))
(define (tri-height x) (sqrt (- (square-script x) (square-script (* .5 x)))))
(define (shape-area shape-type size)
(cond [(symbol=? shape-type 'circle) (* pi (square-script size))]
[(symbol=? shape-type 'square) (square-script size)]
[(symbol=? shape-type 'equilat-tri) (* size (tri-height size))]))
;; assume that fscript could be rewritten as fscript/web, taking an action
;; as a second argument
(define (map fscript L)
(cond [(empty? L) empty]
[(cons? L) (cons (fscript (first L))
(map fscript (rest L)))]))
(define (prompt-read promptstr)
(begin
(printf "~a: " promptstr)
(read)))
(define (prompt-script plist)
(cond [(empty? plist) empty]
[(cons? plist)
(cons (prompt-read (first plist))
(prompt-script (rest plist)))]))
Turn in a single file hwk5.ss (or hwk5.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.
Refer to the Laws of Homework when preparing your solutions.