程序代写代做代考 DNA Assignment 12: Introduction to Monads

Assignment 12: Introduction to Monads
set!
1.
maybe findf
2.
partition
writer
writer
Show pagesource
Log In
(Nothing)
> (findf-maybe symbol? ‘(1 2 c))
(Just ‘c)
> (findf-maybe boolean? ‘(#f 1 2 c))
(Just #f)
> (findf-maybe number? ‘(a b c))
(Nothing)
> (run-writer (partition-writer even? ‘(1 2 3 4 5 6 7 8 9 10)))
((1 3 5 7 9) . (2 4 6 8 10))
> (run-writer (partition-writer odd? ‘(1 2 3 4 5 6 7 8 9 10)))
((2 4 6 8 10) . (1 3 5 7 9))
.danom eht gnisu siht tnemelpmI .trap eulav eht ni od taht seulav eht dna danom eht fo trap retirw eht ni etaciderp eht ssap
ton od taht seulav eht htiw riap dettod a gninruter ,etaciderp a dna tsil a sekat noitcnuf ehT
.snoitcnuf lufesu rehtar emos tnemelpmi ot danom siht esu nac ew ,setirw eseht tneserper ot tsil a esu ew fI .eulav nruter lautca eht morf yletarapes atad etirw ot msinahcem a sedivorp danom ehT
.ssap stnemele s’tsil eht fo enon fi ro etaciderp eht ssap ot tnemele tsil tsomtfel eht rehtie nruter lliw ew ,noitatnemelpmi
ruo nI .stnemele s’tsil eht tset ot hcihw tsniaga etaciderp a dna tsil a sekat noitcnuf tekcaR ehT .erutcel ni detneserp danom eht fo noitinfied eht llaceR
.stceffe edis mrofrep taht serudecorp tekcaR rehtona yna ro esu ton oD .deen dluohs uoy snoitinfied danom eht lla sniatnoc sihT .yrotcerid 113C ruoy ot ti dda dluohs uoY .elfi tkr.sdanom eht esu dluohs uoY
noitcudortnI reldaW lihP
]txt.sremehcs-rof-sdanom/slairotut/hcraeser/namrehd/emoh/ude.uen.scc.www//:ptth[ sdanoM s’remehcS siht pu etorw namreH evaD ]fdp.datsaab/frodrebotkram/srepap/reldaw/ku.ca.de.fni.segapemoh//:ptth[ morf noitanalpxe siht stseggus osla ,seton s’noremaC fo ,noremaC .esu emos fo eb osla yam weiV s’remehcS s’naD … … noitareti reilrae sti dna sdanoM fo weiV s’madA dna naD .krowemoh eht ot tsesolc weh hcihw esoht ylbaborp era seton erutcel s’noremaC
.tnemngissa siht rof esu dluoc uoy seton erutcel fo ecnadnuba na era erehT tnemngissA
.¡±gniht yzzuf mraw¡° naht rehtar ¡±danom¡° mret yracs eht gnisu :ekatsim tseggib ruO
danoM retirW
danoM ebyaM
:tiderc eviecer ot redro nI stnemeriuqeR

3.
power
x n
(define power
(lambda (x n)
(cond
[(zero? n) 1]
[(zero? (sub1 n)) x]
[(odd? n) (* x (power x (sub1 n)))]
[(even? n) (let ((nhalf (/ n 2)))
(let ((y (power x nhalf)))
(* y y)))])))
state
inj-state bind-state
4. x tr xx
powerXpartials
> (run-writer (powerXpartials 2 6))
((2 4 8) . 64)
> (run-writer (powerXpartials 3 5))
((3 9 81) . 243)
> (run-writer (powerXpartials 5 7))
((5 25 125 15625) . 78125)
> ((run-state (replace-with-count ‘o ‘(a o (t o (e o t ((n . m) . o) . f) . t) . r))) 0)
(4 . (a 0 (t 1 (e 2 t ((n . m) . 3) . f) . t) . r))
> ((run-state (replace-with-count ‘o ‘(((h (i s . o) . a) o s o e . n) . m))) 0)
(3 . ((h (i s . 0) . a) 1 s 2 e . n) . m))
> ((run-state (replace-with-count ‘o ‘(o (h (o s . o) . o) . o))) 1)
(6 . (1 (h (2 s . 3) . 4) . 5))
traverse
> (define traverse
(lambda (inj bind f)
(letrec
((trav
(lambda (tree)
(cond
[(pair? tree)
(go-on ([a (trav (car tree))]
[d (trav (cdr tree))])
(inj (cons a d)))]
[else (f tree)]))))
trav)))
5. n (/1n) 0
reciprocal maybe (Nothing) 0
si nehw dna detupmoc eulav yna gninruter ,danom tnemelpmI .lacorpicer on sah taht etoN . yb detupmoc si
eht gnisu
rebmun a fo lacorpicer ehT
.smelborp eerht txen eht ni
gniwollof eht esu ll’uoY
.ereh taht od ll’eW .tluser a sa roivaheb tnereffid teg dna ,noitarepo cidanom dna danom tnereffid a ni pord neht nac uoY .danom eht revo edoc ruoy eziretemarap ot uoy selbane sihT .meht sesu taht edoc eht ni erutcurts gniylrednu eht laever nac ti taht si edoc cidanom tuoba sgniht taen eht fo enO
.raf os nees neeb evah taht s fo rebmun eht htiw fo ecnerrucco yreve ecalper klaw redroerp a aiv ,)sevael eht ta slobmys htiw eert a .e.i( slobmys fo eert yranib a dna lobmys a neviG
. dna htiw skrow dna etats a sesu danom eht taht erutcel morf llaceR danoM etatS
.yaw eht gnola detupmoc tluser laitrap hcae htiw gnola ,eulav larutan a sa rewsna eht nruter dluohs tI .tnenopxe na dna esab a sekat osla hcihw ,erudecorp eht tnemelpmi ,danom retirw eht gnisU
:mhtirogla siht gnisu rewop a ot esab a sesiar taht noitcnuf a , fo noitinfied eht si ereH .srewop regetni ot srebmun gnisiar ylkciuq rof dohtem a si gnirauqs yb noitaitnenopxE
smelborP sdanoM dexiM

> (reciprocal 0)
(Nothing)
> (reciprocal 2)
(Just 1/2)
0
> (define traverse-reciprocal
(traverse Just bind-maybe reciprocal))
> (traverse-reciprocal ‘((1 . 2) . (3 . (4 . 5))))
(Just ((1 . 1/2) . (1/3 . (1/4 . 1/5))))
> (traverse-reciprocal ‘((1 . 2) . (0 . (4 . 5))))
(Nothing)
6. halve
> (run-writer (halve 6))
(() . 3)
> (run-writer (halve 5))
((5) . 5)
> (define traverse-halve
(traverse inj-writer bind-writer halve))
> (run-writer (traverse-halve ‘((1 . 2) . (3 . (4 . 5)))))
((1 3 5) . ((1 . 1) . (3 . (2 . 5))))
7. state/sum run-state
> ((run-state (state/sum 5)) 0)
(5 . 0)
> ((run-state (state/sum 2)) 0)
(2 . 0)
> ((run-state (state/sum 2)) 3)
(5 . 3)
> (define traverse-state/sum
(traverse inj-state bind-state state/sum))
> ((run-state (traverse-state/sum ‘((1 . 2) . (3 . (4 . 5))))) 0)
(15 . ((0 . 1) 3 6 . 10))
.eert eht fo mus lanfi eht sniatnoc etats eht hcihw ni dna ,stnemele eht fo smus laitrap fo gnitsisnoc eert a nruter nac ew ,siht gnisU
.5 si mus eht dna 0 si etats eht ,esac tsrfi eht ni oS .etats laitini eht htiw fo tluser eht llac ew neht ,htiw etats eht mus ot rebmun eht mus/etats evig ew ,woleB .etats tnerruc eht ot rebmun taht dda dna ,eulav eht sa
etats tnerruc eht nruter ,rebmun a nevig nehw ,lliw hcihw noitcnuf a tnemelpmI .mus/etatS
.)2 yb elbisivid ylnaelc ton era hcihw( srebmun ddo eht deggol ev’ew hcihw ni dna ,ecalp ni niamer sddo eht ,devlah neeb evah srebmun neve eht hcihw ni eert a nruter nac ew ,siht gnisU
.)danom retirw eht gnisu( rebmun taht gol osla dna ,ecalp ni rebmun lanigiro eht evael daetsni lliw ,owt yb elbisivid ton si rebmun eht fi ,ro ,rebmun
eht flah danom eht ni nruter lliw rehtie ,rebmun a nevig ,taht noitcnuf eht tnemelpmI .evlaH
. a sniatnoc eert eht fi eruliaf langis daetsni dna ,slacorpicer fo eert a nruter nac ew ,siht gnisU
.danom tsiL eht fo wal ytivitaicossa eht evorP danom tsiL :resaetniarB
.dedivorp

Cont
(define value-of
(lambda (expr env)
(match expr
[(? number?) expr]
[(? boolean?) expr]
[(? symbol?) (apply-env env expr)]
[`(* ,x1 ,x2) (* (value-of x1 env) (value-of x2 env))]
[`(sub1 ,x) (sub1 (value-of x env))]
[`(zero? ,x) (zero? (value-of x env))]
[`(if ,test ,conseq ,alt) (if (value-of test env)
(value-of conseq env)
(value-of alt env))]
[`(capture ,k-id ,body) (callcc (lambda (k)
(value-of body (extend-env k-id k env))))]
[`(return ,k-exp ,v-exp) ((value-of k-exp env) (value-of v-exp env))]
[`(lambda (,id) ,body) (closure id body env)]
[`(,rator ,rand) (apply-proc (value-of rator env) (value-of rand env))])))
Cont
empty-env apply-env extend-env closure apply-proc
value-of value-of-cps value-of value-of-cps
> (define fact-5
‘((lambda (f)
((f f) 5))
(lambda (f)
(lambda (n)
(if (zero? n)
1
(* n ((f f) (sub1 n))))))))
> ((run-cont (value-of-cps fact-5 (empty-env))) (lambda (v) v))
120
> (define capture-fun
‘(* 3 (capture q (* 2 (return q 4)))))
> ((run-cont (value-of-cps capture-fun (empty-env))) (lambda (v) v))
12
and
cvm yb 41:31 90/40/0202 :defiidom tsaL ¡¤ txt.sdanom
.]lmth.sidraT-danoM-lortnoC/scod/0.0.3.0-sidrat/egakcap/gro.lleksah.egakcah//:sptth[ sidraT a esu d’uoy ,emit hguorht drawkcab drawrof og ot tnaw uoy fi ,esruoc fO .elfi ruoy fo mottob eht ta tuo detnemmoc rewsna ruoy evael dluohs uoy ,egaugnal tnereffid a ni eb thgim ti ecniS .]yzal=q?lmth.xedni/yzal/gro.gnal
-tekcar.scod//:ptth[ tekcaR yzaL esu ot evah thgim uoY .ti htiw gnitseretni gnihtemos oD .]/danom -etats-esrever-eht-kcufdnim/01/80/8002/moc.sserpdrow.remlapekul//:sptth[ danom etatS esreveR eht si ereH
emos era ereH .
ruoy fo snoitatneserper ni
dna
dna ,
:ssap dluohs reterpretni ruoy stset htob rof krow dluohs srepleh emas eht fo tsoM .eciohc
nwo ruoy edivorP .
ti llac dna ,
, , , cidanom a etaerc ot danom
eht esU
:ssalc ni etorw naD tahw gnilbmeser reterpretni elyts tcerid a si reterpretni gniwollof ehT reterpretnI danoM SPC
.sdanoM fo weiV s’remehcS A fo 81-61 .pp ees ,danom eht gnisu selpmaxe erom roF .danom noitaunitnoc eht fo noitinfied eht rof elfi tkr.sdanom eht ni kool a ekaT
danom noitaunitnoC :resaetniarB
tresseD tsuJ