程序代写代做代考 DNA interpreter Assignment 5: Parameter-Passing Conventions

Assignment 5: Parameter-Passing Conventions
Show pagesource
Log In
empty-env extend-env apply-env make-closure apply-closure begin2
random
(define value-of
(lambda (exp env)
(match exp
[`,b #:when (boolean? b) b]
[`,n #:when (number? n) n]
[`(zero? ,n) (zero? (value-of n env))]
[`(sub1 ,n) (sub1 (value-of n env))]
[`(* ,n1 ,n2) (* (value-of n1 env) (value-of n2 env))]
[`(if ,test ,conseq ,alt) (if (value-of test env)
(value-of conseq env)
(value-of alt env))]
[`(begin2 ,e1 ,e2) (begin (value-of e1 env) (value-of e2 env))]
[`(random ,n) (random (value-of n env))]
[`,y #:when (symbol? y) (apply-env env y)]
[`(lambda (,x) ,body) (make-closure x body env)]
[`(,rator ,rand) (apply-closure (value-of rator env)
(value-of rand env))])))
Convention
Interpreter Name
val-of-cbv
val-of-cbr
val-of-cbname
val-of-cbneed
functional representation
empty-env extend-env apply-env make-closure apply-closure make-closure
make-closure val-of-cbr val-of-cbv
lambda
zero? sub1 * if begin2 set!
random
.]lmth.sexob/ecnerefer/gro.gnal-tekcar.scod//:ptth[ noitatnemucoD tekcaR eht ees ,meht htiw mrofrep nac uoy snoitarepo eht dna sexob tuoba erom roF .snoitnevnoc gnissap-retemarap tnemelpmi pleh ot sexob esu dluohs uoY
. dna , , , ,reterpretni eht sllac
,
esU .srepleh
dna ,
, , ,
eht tnemelpmi ot deen lliw uoY deen-yb-llac eman-yb-llac ecnerefer-yb-llac eulav-yb-llac
dna dna ,
,egaugnal ruo ot smrof wen owt sdda ti taht tpecxe ,uoy ot railimaf etiuq kool dluohs reterpretni sihT .
.
dna
eldnah osla tsum )owt rehto eht ton( sreterpretni dna ruoY ,noitacilppa , ,selbairav ,srebmun ,snaeloob :gniwollof eht eldnah tsum sreterpretni llA .reterpretni hcae htiw gnola og ot fo snoisrev tnemelpmi ot deen ll’uoy ecnis ,revewoH .reterpretni yreve rof srepleh fo tes emas eht esu nac uoy ,trap tsom eht roF .serusolc dna stnemnorivne htob fo a
:swollof sa sreterpretni ruoy emaN .noitnevnoc gnissap-retemarap tnereffid a gnisu eno hcae ,reterpretni siht fo snoisrev wen ruof tnemelpmi ot si ksat ruoY
.
, , , srepleh evfi sti rof tpecxe etelpmoc si taht reterpretni na si woleB 1 traP
.stset evitsuahxe deredisnoc eb ton dluohs eseht lausu sa ;tnemngissa ruoy tset ot stset tneduts 5a eht esu ot elba eb dluohs uoY .4102 ,6 beF dna 1102 ,92 peS no seton morf snoitairav gnissap-retemarap ta kool htped-ni nA * :tnemngissa siht no krow uoy sa lufesu eb ot secruoser gniwollof eht dnfi yam uoY
stseT detsegguS
tnemngissA
etoN

;; Making sure set! works
> (val-of-cbr
‘((lambda (x) (begin2 (set! x #t)
(if x 3 5))) #f)
(empty-env))
3
;; Returns 4 under CBR…
> (val-of-cbr
‘((lambda (a)
((lambda (p)
(begin2
(p a)
a)) (lambda (x) (set! x 4)))) 3)
(empty-env))
4
;; …but returns 3 under CBV.
> (val-of-cbv
‘((lambda (a)
((lambda (p)
(begin2
(p a)
a)) (lambda (x) (set! x 4)))) 3)
(empty-env))
3
;; returns 44 under CBR…
(test “interesting-cbr-2”
(val-of-cbr
‘((lambda (f)
((lambda (g)
((lambda (z) (begin2
(g z) z))
55))
(lambda (y) (f y)))) (lambda (x) (set! x 44)))
(empty-env))
44)
;; …but returns 55 under CBV! You can change the “begin2” to
;; “begin” and evaluate this in the Racket REPL as evidence that
;; Racket uses CBV.
> (val-of-cbv
‘((lambda (f)
((lambda (g)
((lambda (z) (begin2
(g z)
z))
55))
(lambda (y) (f y)))) (lambda (x) (set! x 44)))
(empty-env))
55
;; Returns 44 under CBR…
> (val-of-cbr
‘((lambda (swap)
((lambda (a)
((lambda (b)
(begin2
((swap a) b)
a)) 44)) 33))
(lambda (x)
(lambda (y)
((lambda (temp)
(begin2
(set! x y)
(set! y temp))) x))))
(empty-env))
44
;; …but returns 33 under CBV.
> (val-of-cbv
‘((lambda (swap)
((lambda (a)
((lambda (b)
(begin2
((swap a) b)
a)) 44)) 33))
(lambda (x)
(lambda (y)
((lambda (temp)
(begin2
(set! x y)
(set! y temp))) x))))
(empty-env))
33
> (define random-sieve
‘((lambda (n)
(if (zero? n)
(if (zero? n) (if (zero? n) (if (zero? n) (if (zero? n) (if (zero? n) (if (zero? n) #t #f) #f) #f) #f) #f) #f)
(if (zero? n) #f (if (zero? n) #f (if (zero? n) #f (if (zero? n) #f (if (zero? n) #f (if (zero? n) #f #t))))))))
(random 2)))
;; call-by-name
;;P(false positive) <= .01 > (val-of-cbname random-sieve (empty-env))
#f
.edoc ruoy yfirev pleh ot stset nwo ruoy etirw syawla ;selpmaxe wef a tsuj era stset eseht ,rebmemeR

val-of-cbv
> (define cons-test
‘(let ((fix (lambda (f)
((lambda (x) (f (lambda (v) ((x x) v))))
(lambda (x) (f (lambda (v) ((x x) v))))))))
(let ((map (fix (lambda (map)
(lambda (f)
(lambda (l)
(if (null? l)
‘()
(cons^ (f (car^ l))
((map f) (cdr^ l))))))))))
(let ((take (fix (lambda (take)
(lambda (l)
(lambda (n)
(if (zero? n)
‘()
(cons (car^ l)
((take (cdr^ l)) (sub1 n))))))))))
((take ((fix (lambda (m)
(lambda (i)
(cons^ 1 ((map (lambda (x) (add1 x))) (m i)))))) 0)) 5)))))
> (val-of-cbv cons-test (empty-env))
(1 2 3 4 5)
;; call-by-need
> (val-of-cbneed random-sieve (empty-env))
#t
;; Does not terminate with val-of-cbr or val-of-cbv — try it!
> (val-of-cbname
‘((lambda (z) 100)
((lambda (x) (x x)) (lambda (x) (x x))))
(empty-env))
100
cvm yb 23:51 60/20/0202 :defiidom tsaL ¡¤ txt.5-tnemngissa
.]/moc.lleksahauoynrael//:ptth[ !doog taerg rof lleksaH a uoy nrael neht ,llew ,xatnys eht htiw elbuort evah uoy fI .sseug I ,ti htiw ygniht teehsdaerps a ekam rO .ecnatsni rof ,reterpretni deen-yb-llac ruoy ni gnikrow ti teg dluoc uoY .ti htiwtaengnihtemosoddna]dm.beom-beol/retsam/bolb/selcitra/nehcuq/moc.buhtig//:sptth[sihtdaeR.snoitcnufloocera)beoMdna(beoL
.?llun dna ,tel ,tsil ytpme ,1dda htiw gnola reterpretni ruoy ot )¡±rdc¡° ,¡±rac¡° ,¡±snoc¡° raluger( snoisrev tcirts eht dda dluohs uoY .¡±^snoc¡° no etarepo taht )¡±^rdc¡° dna ¡±^rac¡°( rdc dna rac fo snoisrev gnidnopserroc eht etaerc osla dluohs uoY .)ylizal meht setaulave ti ,sdrow rehto ni( yltcirts stnemugra sti etaulave TON seod taht ¡±^snoc¡° dda ,reterpretni ¡±vbc-fo-lav¡° ruoy oT .reterpretni ruoy ot ti gnidda eb lliw uoy tub ,tekcaR ni orcam a sa tnemelpmi ot ysae si stnemugra sti etaulave ton seod taht snoC .serutcurts atad yzal rof deen eht tuo stniop hcihw ,]fdp.44RT/stroperhcet/bup/ude.anaidni.sc.www//:ptth[ “stnemugrA sti etaulavE ton dluohs SNOC” repap eht saw LP ni s0791 eht gnirud srepap laitneuflni ylhgih eht fo enO
tresseD tsuJ
resaetniarB