Assignment 2: Free, Bound, and Lexical Address
lambda
Functions defined in Part 1 can(actually, should) be used as helper functions for the later problems.
nth-cdr
nth-cdr
union lang.org/reference/pairs.html?q=memv&q=list-
memv [http://docs.racket-
list-ref
Show pagesource
Log In
(: list-ref (All (A) ( (Listof A) Number A))) (define list-ref
(lambda (ls n)
(letrec
([nth-cdr : ( Number (Listof A)) (lambda (n)
;; complete the definition
)])
(car (nth-cdr n)))))
> (list-ref ‘(a b c) 2)
c
> (list-ref ‘(a b c) 0)
a
s’tekcaR esu ot lufpleh ti dnfi yam uoY .stsil tupni owt eht fo noinu eht gniniatnoc tsil a snruter dna ,setacilpud on htiw stsil owt sekat taht erudecorp a tset dna enfieD .2
.noitinfied ruoy ni atad dab redisnoc ton deen uoy ,rebmemeR
.tnemmoc a gniniatnoc ydob noitcnuf eht gnitelpmoc dnoyeb edoc dedivorp eht yfidom ot deen ton dluohs uoY .yltcerroc krow gniwollof eht taht os , fo noitatnemelpmi evisrucer-yllarutan a htiw fer -tsil fo noitinfied eht etelpmoC .gnissim si fo edis dnah-thgir eht si taht noitcnuf eht fo ydob ehT
.]92%92%fer-tsil_.92%lenrek52~32~_.etouq82%82%_.fed82%#vssa=q&vmer=q&fer -tsil=q?lmth.sriap/ecnerefer/gro.gnal-tekcar.scod//:ptth[ fer-tsil s’tekcaR ot ylralimis etarepo ot dednetni si tI .noitcnuf eht fo noitinfied laitrap gniwollof eht redisnoC .1
¡ú
rehserfeR noisruceR larutaN :1 traP
snoisserpxe suluclac adbmal owt fo snoitacilppa ydob eno yltcaxe evah dna tnemugra eno yltcaxe ekat taht snoisserpxe
selbairav :fo tsisnoc snoisserpxe suluclac-adbmal taht emussa ,tnemngissa siht fo sesoprup eht roF
.lufesu]92%92%vmer_.92%tkr..tsilF2%etavirpF2%tekcar_.bil82%82%_.fed82%#vssa=q&vmer=q ?lmth.sriap/ecnerefer/gro.gnal-tekcar.scod//:ptth[ vmer dna ,]92%92%vssa_.92%tkr..tsilF2%etavirpF2%tekcar_.bil82%82%_.fed82%#vssa=q?lmth.sriap/ecnerefer/gro.gnal -tekcar.scod//:ptth[ vssa ,]lmth.tel/ecnerefer/gro.gnal-tekcar.scod//:sptth[ certel dnfi yam uoY
.evitsuahxe ton era sesac tset dedivorp eht tub ,detrats uoy teg ot esicrexe hcae rof sesac tset emos dedivorp evah eW .tnemngissa ruoy gnittimbus erofeb snoitulos ruoy tset tsum uoY
tnemngissa siht rof senilediuG .cigol a sa esu sti naht metsys eht fo snoitacilppa rehto eb ,deedni ,yam erehT
¡ú
tnemngissA
ref&q=remv&q=assv#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._memv%29%29]
> (union ‘() ‘())
()
> (union ‘(x) ‘())
(x)
> (union ‘(x) ‘(x))
(x)
> (union ‘(x y) ‘(x z))
(x y z)
pred extend
extend eqv? x pred
x pred
> ((extend 1 even?) 0)
#t
> ((extend 1 even?) 1)
#t
> ((extend 1 even?) 2)
#t
> ((extend 1 even?) 3)
#f
> (filter (extend 1 even?) ‘(0 1 2 3 4 5))
(0 1 2 4)
> (filter (extend 3 (extend 1 even?)) ‘(0 1 2 3 4 5))
(0 1 2 3 4)
> (filter (extend 7 (extend 3 (extend 1 even?))) ‘(0 1 2 3 4 5))
(0 1 2 3 4)
s x
sx walk-symbol
x
walk-symbol
x
s
((a . 5) (b . (1 2)) (c . a))
> (walk-symbol ‘a ‘((a . 5)))
5
> (walk-symbol ‘a ‘((b . c) (a . b)))
c
> (walk-symbol ‘a ‘((a . 5) (b . 6) (c . a)))
5
> (walk-symbol ‘c ‘((a . 5) (b . (a . c)) (c . a)))
5
> (walk-symbol ‘b ‘((a . 5) (b . ((c . a))) (c . a)))
((c . a))
> (walk-symbol ‘d ‘((a . 5) (b . (1 2)) (c . a) (e . c) (d . e)))
5
> (walk-symbol ‘d ‘((a . 5) (b . 6) (c . f) (e . c) (d . e)))
f
must
match
.ssalc ni detartsnomed fo serutaef eht fo esu ekam ot deen ylno dluohs uoY .snoisserpxe suluclac-adbmal no noisrucer gnimrofrep eb ot tcepxe dluohs uoy ,trap tsom eht roF .llew sa uoy ot esu fo 1 traP morf snoitcnuf eht fo emos dnfi yam uoY .llew sa ti htiw reisae eb thgim sresaetniarb ehT .smelborp gniniamer eht fo hcae ni hctam esu uoy ,detats esiwrehto sselnU
.lufesu]92%92%vssa_.92%tkr..tsilF2%etavirpF2%tekcar_.bil82%82%_.fed82%#vssa=q ?lmth.sriap/ecnerefer/gro.gnal-tekcar.scod//:ptth[ vssa dnfi thgim uoY . nruter dluohs neht ,noitaicossa on sah fI . ni deklaw eb tsum oot ti ,lobmys a si eulav detaicossa eht fI . htiw detaicossa eulav eht rof hguorht hcraes dluohs erudecorp ruoY
:tsil noitaicossa na si gniwollof eht ,elpmaxe roF .seulav detaicossa fo sriap fo tsil a si tsil noitaicossa nA . tsil noitaicossa na dna lobmys a sekat taht erudecorp a tset dna enfieD .4
. yfsitas ro ot era taht sgniht esoht yb yltcaxe defisitas eb dluohs etaciderp denruter ehT .etaciderp rehtona eb dluohs snruter
tahW ).tnemngissa suoiverp eht morf meht esu ot woh dna era setaciderp tahw llaceR( .etaciderp a si tnemugra dnoces ehT . dna yas ,stnemugra owt sekat taht erudecorp a tset dna enfieD .3
sserddA lacixeL ,dnuoB ,eerF :2 traP
.rettam ton seod rewsna ruoy ni stnemele eht fo redro eht ,niagA .noitinfied siht rof
>lumbda
not
lumbda
union
lambda->lumbda
lambda
lambda
> (lambda->lumbda ‘x)
x
> (lambda->lumbda ‘(lambda (x) x))
(lumbda (x) x)
> (lambda->lumbda ‘(lambda (z) ((lambda (y) (a z)) (h (lambda (x) (h a))))))
(lumbda (z) ((lumbda (y) (a z)) (h (lumbda (x) (h a)))))
> (lambda->lumbda ‘(lambda (lambda) lambda))
(lumbda (lambda) lambda)
> (lambda->lumbda ‘((lambda (lambda) lambda) (lambda (y) y)))
((lumbda (lambda) lambda) (lumbda (y) y))
> (lambda->lumbda ‘((lambda (x) x) (lambda (x) x)))
((lumbda (x) x) (lumbda (x) x))
vars
var-occurs?
vars
unique-vars
vars
occurs
lambda-
not
> (var-occurs? ‘x ‘x)
#t
> (var-occurs? ‘x ‘(lambda (x) y))
#f
> (var-occurs? ‘x ‘(lambda (y) x))
#t
> (var-occurs? ‘x ‘((z y) x))
#t
> (vars ‘x)
(x)
> (vars ‘(lambda (x) x))
(x)
> (vars ‘((lambda (y) (x x)) (x y)))
(x x x y)
> (vars ‘(lambda (z) ((lambda (y) (a z))
(a z h h a)
(h (lambda (x) (h a))))))
> (unique-vars ‘((lambda (y) (x x)) (x y)))
(x y)
> (unique-vars ‘((lambda (z) (lambda (y) (z y))) x))
(z y x)
> (unique-vars ‘((lambda (a) (a b)) ((lambda (c) (a c)) (b a))))
(c b a)
var-occurs-free?
> (var-occurs-free? ‘x ‘x)
#t
> (var-occurs-free? ‘x ‘(lambda (y) y))
#f
> (var-occurs-free? ‘x ‘(lambda (x) (x y)))
#f
> (var-occurs-free? ‘x ‘(lambda (x) (lambda (x) x)))
#f
> (var-occurs-free? ‘y ‘(lambda (x) (x y)))
.esiwrehto f# dna ,noisserpxe taht ni eerf srucco elbairav taht fi t# snruter dna noisserpxe suluclac-adbmal a dna lobmys a sekat taht erudecorp a tset dna enfieD .9
.noitinfied ruoy ni esU .setacilpud nruter ton seod tub ekil sevaheb taht dellac fo noitacfiidom a tset dna enfieD .8
.rettam seod rewsna ruoy ni selbairav eht fo redro eht dna ,
fo noitacfiidom drawrofthgiarts a eb dluohs sihT .noisserpxe eht ni rucco taht selbairav lla gniniatnoc tsil a snruter dna noisserpxe suluclac-adbmal a sekat taht erudecorp a tset dna enfieD .7
.ecnerrucco elbairav a sa tnuoc seod retemarap lamrof a ,su rof :esnes lacinhcet sti ni rucco drow eht esu ew ssalc siht ni eromreverof dna ereH .noisserpxe eht ni elbairav taht rehtehw gnirewsna naeloob a snruter dna noisserpxe suluclac-adbmal a dna eman elbairav a sekat taht erudecorp a tset dna enfieD .6
.)enola tfel eb dluohs elbairav a sa fo secnerrucco eciton( drow eht htiw decalper neeb sah drowyek a sa hcae taht noitpecxe eht htiw degnahcnu noisserpxe eht snruter dna noisserpxe suluclac-adbmal a sekat taht erudecorp a tset dna enfieD .5
#t
#f
var-occurs-bound?
> (var-occurs-bound? ‘x ‘x)
#f
> (var-occurs-bound? ‘x ‘(lambda (x) x))
#t
> (var-occurs-bound? ‘y ‘(lambda (x) x))
#f
> (var-occurs-bound? ‘x ‘((lambda (x) (x x)) (x x)))
#t
> (var-occurs-bound? ‘z ‘(lambda (y) (lambda (x) (y z))))
#f
> (var-occurs-bound? ‘z ‘(lambda (y) (lambda (z) (y z))))
#t
> (var-occurs-bound? ‘x ‘(lambda (x) y))
#f
> (var-occurs-bound? ‘x ‘(lambda (x) (lambda (x) x)))
#t
unique-free-vars
unique-vars
> (unique-free-vars ‘x)
(x)
> (unique-free-vars ‘(lambda (x) (x y)))
(y)
> (unique-free-vars ‘((lambda (x) ((x y) e)) (lambda (c) (x (lambda (x) (x (e c)))))))
(y e x)
((lambda (x) ((x y) e)) (lambda (c) (x (lambda (x) (x (e c)))))))
unique-bound-vars
> (unique-bound-vars ‘x)
()
> (unique-bound-vars ‘(lambda (x) y))
()
> (unique-bound-vars ‘(lambda (x) (x y)))
(x)
> (unique-bound-vars ‘((lambda (x) ((x y) e)) (lambda (c) (x (lambda (x) (x (e c)))))))
(x c)
> (unique-bound-vars ‘(lambda (y) y))
(y)
> (unique-bound-vars ‘(lambda (x) (y z)))
()
> (unique-bound-vars ‘(lambda (x) (lambda (x) x)))
(x)
lambda
lambda
#t
> (var-occurs-free? ‘y ‘((lambda (y) (x y)) (lambda (x) (x y))))
#t
> (var-occurs-free? ‘x ‘((lambda (x) (x x)) (x x)))
#t
:noisserpxe gniwollof eht ni ,elpmaxe roF .srucco ti erehw ecalp eht dna )retemarap lamrof eht sa nwonk osla( dnuob si elbairav eht erehw ecalp eht neewteb s fo rebmun eht si elbairav a fo sserdda lacixel eht ,tnemugra eno ylno evah s erehw tekcaR fo tesbus a nI .31
.selbairav etacilpud niatnoc ton tsum tsil eht tub ,rettam t’nseod redrO .noisserpxe tupni eht ni dnuob rucco taht selbairav eht lla fo tsil a snruter dna noisserpxe suluclac-adbmal a sekat taht erudecorp a tset dna enfieD .21
.snoisserpxe suluclac-adbmal fo tsil a ton ,)noitacilppa erudecorp a( noisserpxe suluclac-adbmal elgnis a si
.tniop gnitrats a sa fo noitinfied eht esu ot lufpleh ti dnfi yam uoY .selbairav etacilpud niatnoc ton tsum tsil eht tub ,rettam t’nseod redrO .noisserpxe taht ni eerf rucco taht selbairav eht lla fo tsil a snruter dna noisserpxe suluclac-adbmal a sekat taht erudecorp a tset dna enfieD .11
.esiwrehto dna ,noisserpxe eht ni dnuob srucco elbairav taht fi snruter dna noisserpxe suluclac-adbmal a dna lobmys a sekat taht erudecorp a tset dna enfieD .01
,evoba elpmaxe driht eht ni taht etoN
o lambda
o o
cadr
car
var
lex
> (lex ‘(lambda (x) x) ‘())
(lambda (var 0))
> (lex ‘(lambda (y) (lambda (x) y)) ‘())
(lambda (lambda (var 1)))
> (lex ‘(lambda (y) (lambda (x) (x y))) ‘())
(lambda (lambda ((var 0) (var 1))))
> (lex ‘(lambda (x) (lambda (x) (x x))) ‘())
(lambda (lambda ((var 0) (var 0))))
> (lex ‘(lambda (y) ((lambda (x) (x y)) (lambda (c) (lambda (d) (y c))))) ‘())
(lambda ((lambda ((var 0) (var 1))) (lambda (lambda ((var 2) (var 1))))))
> (lex ‘(lambda (a)
(lambda (b)
(lambda (c)
(lambda
(lambda
(lambda
(lambda
(lambda
(lambda
(lambda
(lambda
(lambda (a)
(lambda (b)
(lambda (d)
(lambda (a)
(lambda (e)
(((((a b) c) d) e) a))))))))) ‘())
((((((var 1) (var 3)) (var 5)) (var 2)) (var 0)) (var 1))))))))))
> (lex ‘(lambda (a)
(lambda (b)
(lambda (c)
(lambda
(lambda
(lambda
(lambda
(lambda
(lambda
((lambda
(lambda
(lambda (w)
(lambda (x)
(lambda (y)
((lambda (a)
(lambda (b)
(lambda (c)
(((((a b) c) w) x) y))))
(lambda (w)
(lambda (x)
(lambda (y)
(((((a b) c) w) x) y))))))))))) ‘())
(lambda
((((((var 2) (var 1)) (var 0)) (var 5)) (var 4)) (var 3)))))
(lambda
(lambda
(lambda
((((((var 8) (var 7)) (var 6)) (var 2)) (var 1)) (var 0))))))))))))
(lambda (o)
(lambda (r)
(lambda (s)
(lambda (p)
(lambda (g)
o)))))
.selbairav dnuobnu redisnoc ton deen uoY .elbairav decnerefer eht fo sserdda lacixel eht si esohw dna lobmys eht si esohw stnemele owt fo stsil yb decalper secnerefer elbairav dnuob lla htiw noisserpxe emas eht snruter dna ,)tsil ytpme eht sa strats hcihw( rotalumucca na dna noisserpxe suluclac-adbmal a sekat taht erudecorp a tset dna enfieD
.mottob eht ta fo ecnerrucco eht dna pot eht ta retemarap lamrof eht neewteb snoisserpxe
ruof era ereht esuaceb ,4 fo sserdda lacixel a sah tI .ecnerrucco dnuob a si mottob yrev eht ta ehT
walk-symbol
‘((z . 26) (y . z) (x . y) … (b . c) (a . b))
box!
occurs-both? x
solution
box unbox set-
var-
values one-pass
var-occurs-both?
walk-symbol-update
a
> ;; (define a-list `((c . ,(box 15)) (e . ,(box ‘f)) (b . ,(box ‘c)) (a . ,(box ‘b))))
> ;; the above definition won’t work in typed racket, use the one below.
> (begin
(: a-list (Listof (Pairof Symbol (Boxof Any))))
(define a-list
(map (lambda ([pr : (Pairof Symbol Any)])
(cons (car pr) (box (cdr pr))))
‘((c . 15) (e . f) (b . c) (a . b)))))
> a-list
((c . #&15) (e . #&f) (b . #&c) (a . #&b))
> (walk-symbol-update ‘a a-list)
15
> a-list
((c . #&15) (e . #&f) (b . #&15) (a . #&15))
> (walk-symbol-update ‘a a-list)
15
> a-list
((c . #&15) (e . #&f) (b . #&15) (a . #&15))
.) enfied ottideent’nowylbaborpuoyhguoht]92%92%seulav-htiw-llac_.92%lenrek52~32~_.etouq82%82%_.fed82%#seulav -htiw-llac=q?lmth.seulav/ecnerefer/gro.gnal-tekcar.scod//:ptth[seulav-htiw-llacdna(]92%92%seulav -tel_.92%lenrek52~32~_.etouq82%82%_.mrof82%#seulav=q?lmth.tel/ecnerefer/gro.gnal-tekcar.scod//:ptth[ seulav-teldna]92%92%seulav_.92%lenrek52~32~_.etouq82%82%_.fed82%#seulav=q?lmth.seulav/ecnerefer/gro.gnal -tekcar.scod//:ptth[ seulav no noitatnemucod tekcaR eht ees dluohs uoy ,seulav elpitlum nruter ot redro nI
.rotalumucca na esu ton dluohs uoy dna ,eciwt atad emas eht revo rucer ton dluohs uoy gninaem ,
a eb dluohs noitulos ruoY .noisserpxe eht ni dnuob srucco rav eht rehtehw gnirewsna naeloob a si dnoces eht dna ,noisserpxe eht ni eerf srucco elbairav eht rehtehw gnirewsna naeloob a si hcihw fo tsrfi
eht , owt snruter dna ,noisserpxe suluclac-adbmal a dna elbairav a sekat taht
etaciderp a enfieD .noisserpxe emas eht ni dnuob rucco dna eerf rucco htob nac elbairav A .51
.melborp siht rof snoitcnuf
dna, , ehttuobanoitamrofnirof]lmth.sexob/ecnerefer/gro.gnal-tekcar.scod//:ptth[sexob tuoba noitatnemucoD tekcaR eht tlusnoc ot lufesu ti dnfi lliw uoY .ti fo noitrop a egnahc ot detnaw ew emit hcae erutcurts atad eritne eht ypoc-er ot deriuqer neeb evah dluow ew )tceffe-edis emos ro( sexob tuohtiW
.elpmaxe gniwollof eht eeS .retsaf eb lliw pukool eht ,niaga lobmys emas taht klaw ot evah ew fi ,suhT .eulav lanfi eht sniatnoc osla esoht fo hcae fo edis dnah-thgir eht taht os ,yaw eht gnola hguorht klaw ot dah ew sexob ni seulav eht egnahc osla nac ew ,htiw detrats ew lobmys eht rof eulav lanfi eht dnfi ot tsil noitaicossa eht klaw ew nehw ,nehT .sniatnoc xob eht eulav eht egnahc nac ew gninaem ,secnerefer yromem elbatumerasexoB.eulavasniatnoctaht]lmth.sexob/ecnerefer/gro.gnal-tekcar.scod//:ptth[xobasyawla si noitaicossa hcae fo edis dnah-thgir eht taht hcus tsil noitaicossa ruo etirw lliw eW .rennam gniwollof eht ni ,noisserpmoc-htap htiw tnemelpmi lliw ew ,nedrub siht etaivella yllaitrap oT
.tsil noitaicossa gniwollof eht ni eciwt klaw ot od ot evah dluow uoy krow eht redisnoC .gnol ylirartibra eb yam sniahc esrevrep niatrec sa ,evisnepxe ylevitibihorp emoceb yam emit yreve niahc lluf eht gniklaW .tsil noitaicossa taht ni seulav pu kool yltneuqerf ew taht enigamI .melborp eht fo oiranecs eht niaga redisnoC .41
.tuo ti erugfi ot deen uoy noitamrofni eht lla evah ydaerla uoy taht ,sserdda lacixel eht ni llfi ot deen uoy erehw noisserpxe eht ni noitisop elbairav a ot teg uoy nehw taht erusne ,si tahT .eert eht pu kcab og ot gnivah tuohtiw ti od ot woh tuo erugfi dna yrt dna ,gniod er’uoy si ti tahw tuoba kniht nehT .nwod
yaw ruoy gnikrow dna noisserpxe eht fo pot eht morf gnitrats ,yllacinahcem tsomla ti od dna yrT .meht ni rucco taht selbairav emos fo sesserdda lacixel eht dnfi dnah yb neht dna ,repap dna nep no snoisserpxe adbmal emos dliub ot si noitseggus yM .melborp siht hcaorppa nac uoy syaw fo rebmun a era erehT
tresseD tsuJ
sresaetniarB
> (var-occurs-both? ‘x ‘(lambda (x) (x (lambda (x) x))))
#f
#t
> (var-occurs-both? ‘x ‘(x (lambda (x) x)))
#t
#t
> (var-occurs-both? ‘x ‘(lambda (y) (x (lambda (x) x))))
#t
#t
> (var-occurs-both? ‘x ‘(lambda (x) (lambda (x) (x (lambda (x) x)))))
#f
#t
> (var-occurs-both? ‘x ‘(lambda (x) (lambda (y) (lambda (x) (x (lambda (x) x))))))
#f
#t
> (var-occurs-both? ‘x ‘(lambda (y) (lambda (x) (lambda (z) (lambda (x) (x (lambda (x) x)))))))
#f
#t
cvm yb 13:22 92/10/0202 :defiidom tsaL ¡¤ txt.2-tnemngissa