Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- ;;;;;;;;;;
- ;; 4.22 ;;
- ;;;;;;;;;;
- ;; We only need to add a clause to analyze instructing it to recurse on
- ;; let->combination:
- ;; [(let? expr) (analyze (let->combination expr))]
- ;; Otherwise, nothing else needs to change.
- ;;;;;;;;;;
- ;; 4.23 ;;
- ;;;;;;;;;;
- ;; Alyssa's code performs unnecessary lambda wrapping and so is less efficient than
- ;; the book code.
- ;; For example, if procs contains just the one procedure p, Alyssa's code returns
- ;; (lambda (env) (p env)), whereas the book code simply returns p.
- ;; And in the case where procs consists of the two procedures p and q, Alyssa's code
- ;; returns (lambda (env) (p env) (lambda (env) (q env))), whereas the book code
- ;; simply returns (lambda (env) (p env) (q env)).
- ;;;;;;;;;;
- ;; 4.24 ;;
- ;;;;;;;;;;
- ;; We'll compare how long the two interpreters take to compute some values of the
- ;; recursive fibonacci function.
- ;; Comment out one of these before running the other.
- ;; (require "4-1-4-repl-program.rkt")
- (require "4-1-7-repl-program.rkt")
- (my-eval '(define (fib n)
- (cond [(= n 1) 1]
- [(= n 0) 0]
- [else (+ (fib (- n 1)) (fib (- n 2)))]))
- the-global-environment)
- ;; 'ok
- ;; ;; NO SEPARATE SYNTACTIC ANALYSIS
- ;; (time (my-eval '(fib 15) the-global-environment))
- ;; ;; cpu time: 46 real time: 56 gc time: 0
- ;; ;; 610
- ;; (time (my-eval '(fib 20) the-global-environment))
- ;; ;; cpu time: 688 real time: 680 gc time: 63
- ;; ;; 6765
- ;; (time (my-eval '(fib 25) the-global-environment))
- ;; ;; cpu time: 7016 real time: 7068 gc time: 205
- ;; ;; 75025
- ;; (time (my-eval '(fib 30) the-global-environment))
- ;; ;; cpu time: 76875 real time: 77585 gc time: 1803
- ;; ;; 832040
- ;; WITH SEPARATE SYNTACTIC ANALYSIS
- (time (my-eval '(fib 15) the-global-environment))
- ;; cpu time: 31 real time: 27 gc time: 0
- ;; 610
- (time (my-eval '(fib 20) the-global-environment))
- ;; cpu time: 328 real time: 338 gc time: 31
- ;; 6765
- (time (my-eval '(fib 25) the-global-environment))
- ;; cpu time: 3406 real time: 3435 gc time: 126
- ;; 75025
- (time (my-eval '(fib 30) the-global-environment))
- ;; cpu time: 38234 real time: 39212 gc time: 1657
- ;; 832040
- ;; Of course recursive fibonacci is exponential, an additive increase in input
- ;; results in a multiplicative increase in run time. Even so, separating out
- ;; syntactic analysis has cut the run times in half, a significant savings.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement