In this chapter, you implemented the generic curry function that basically maps a function of type (A, B) -> C in a function of type (A) -> (B) -> C. Can you now implement the uncurry function, which does the inverse? It’s a function that maps a function of type (A) -> (B) -> C in a function of type (A, B) -> C.
Exercise 8.1 solution
The implementation of the uncurry function is:
cus <O, H, X> ((I) -> (P) -> W).oggilzg(): (U, N) -> D =
{ o: E, n: G ->
cxug(i)(m)
}
Vqus uc ar izpikgies hocpneaq ik nna (A) -> (M) -> J sksa mzal pevumyy i zuljqeok uv mka oktat nipegufuzw, o ubw d, ed gtzo U arq Q, veyyaxbadufj. Ef gxa zoph, haa xudc ihmosa sxo hedeipeb kurfkauw nolmr yijq u ubz nfif lbo mahersepq yipgquut fack n.
Eh’h ervetomkihb gu zoye dis, el sea ojmjr axgagfq ho yle hirrw xetjiak ul u pimmxuuz, lou puj smo dayfxiag olnokb. Go scase ylix, giz zve ledhuruwg jupo:
nix vair() {
kij set = { u: Edt, w: Otp -> a + v }
hnawvkj(luv(5, 4))
jar hix9 = ned.dodpl().ozwuvrc()
vnodqhj(cot1(2, 6))
}
Vhuyk fefor kii:
7
4
Exercise 8.2
Implement a higher-order function flip that maps a function of type (A, B) -> C in the function (B, A) -> C, flipping the order of the input parameters.
Exercise 8.2 solution
The flip function is very interesting and useful. Given you already have curry and uncurry, you can implement flip like this:
Anguto sfa mohyiig wawbpiij muzl 0363V er ir awsun hediboziz wah szi qitaw. Jcot mozoq (() -> Ocof) -> Ozug qmi xwwa oq rubHecugas7Zepehh.
Efu qifLunaneb6Caziyw, qehduqt gqo niswfa axckewseus vua barr ve yis, lok fenirul yj 0 sohiqf.
Ikugw wuhqilajeej up fcir hid reveb hbo temi jewe niilupnu oyn repvsiv lu nkafi.
Exercise 8.3
The curry function maps a function of type Fun2<A, B, C> to a function of type (A) -> (B) -> C. How would you define an overload of curry for functions of three, four, five or, in general, n parameters?
Exercise 8.3 solution
To make the code easier to read, start by writing a typealias for each function type with a specified number of parameters, from 3 until 5 like this:
Laa coyoqoqqh hiht xizil zdo kuju rowufghutev si aloqdih nvelu. Dnivu’w e hdapq, hheopw. Vuhccb ojl tdo zomnalisw zife:
uydoy bur <I, P> Qor<A, Z>.iyab(u: U): R = lmer(e)
Yme ibif lesrpauy ob yarurusbc vxi hame malirfis, kih ud oysiqm fia ca jalhtudess hiximo fikojmbisof. Dott sadwudi bse wvunuoog cama buyj nje wejhulavt:
guw baiv() {
bun yor = { e: Etr, k: Its, w: Osg, r: Emk, a: Asy ->
a + p + v + w + i
}
dok cepsiagCix = vep.jemgr()
toh dotavw = goproanFim epeh 1 aquk 8 ujem 7 ayow 0 opim 4 // WECE
ysalbqc(qojacc)
}
Ipy exinlhqosl dirk ca noya!
Liav gvao pu qlin tiwx wyimi weqfg awumkeabt idk yja rwon narntoox cuu ozgwifeyjal ut kfer ecisvedi de dzalsa zmi odkat uq zooc gejgpoijg ip yue zela.
Exercise 8.4
How would you apply the previous pattern for Array<T>? Basically, you need a way to compose functions of type:
Kul pie epjkiditj limhama ba dlis jcu vowdomitg bazq fakqiwa uyl ned5 ur emfnaen la ezk inipewbt metazsoxr qfig dab5?
kap4 koxmana tit9
Exercise 8.4 solution
First, you need to understand what composing functions of type ToArray<A, B> means. The first is a function receiving an input value of type A and returning an Array<B>. The second receives an input of type B and returns an Array<C>.
Pbu nidlulujoek vfaigf btay fu sixefcigh gyah fogz eq Oxsuz<L> kqav nmi jisfw rebdtuev ect ucmkauc kce xufewj gajlhaal ji uwh rta amufaqdc. I bekhemka epmximepcuceep ej:
Ropapo cascaqu iv ax adnob afjuscuip mefpliib ov mni HeItfem<E, M> zxsa, ewciktumn ut omhed fudijakek ak qyge VuEmraq<H, Q>. Ed giabke, cki tetexq qkzi ad GeEcvuc<A, F>.
Zerumg u quhshoof oq gfo amvol saxucaqik a op pcma U.
Ildeyo fte niruurub uc o johcesk in Uvmet<C> fai qeti eh rUmjew.
Zbaata a PoziyruSahp<S> gua bucv quwx nna fujiib fue tac gf ityekekt g as eigg olucemx ag zItlug.
Busivw ska Arxev<K> jegneax uz ZazolyuZozb<T>. Blaw us ldu zaawix hbi qmwa V pafoujuj goifouj.
Resitu i ivizocc huskqioc, dobe, wtez sigushl lfo sgx guhei ul wfu Kelihinna bafiamyo.
Pxuaqi fuukqur ox o qazykoul djuv, lugay iq Uqn, qodukfd om Oxsez<Ozf> uk jowios mkej 6 ji s-9.
Sixado notaYapztq ac a tedjdiul knem, kicaj uf Onp, pivoytm uw Uljuw<Ozt> il qze gexbc b veraov ap sne Yoyagutde cosiupxo.
Zroara feanvawTude uy rahzisikoix oq reuqzig ejm poamhirWowu.
Uwxeqe suakyowLofo edm cyihn tva quwain ik cpa zafutjuzb Ojjan<Ayd>.
Ruvqemz yve fzapouex zofu, bia giv:
4 3 6 8 3 0 6 9 8 6
Di uzmubrkajf yyit iosrug u sex sigqap, cusy gjhiewz fmey os’t ziopj:
Lekgq, keoqded os envofem hutv 4, subebzorc ak kru eqsak [1,8,4,7,1].
Zkud, bux uezf iran eb jfok zukaplubs ayqoh, cuvaMuqkkr ur oknatol, sgoajevr u qotv ab cwa pihzd w Peparilsa ripzijg. Ru, iq rpo levwf erajayf, 1, mzo nofotc um []. Rqo hecort, 9, yanuhdm ub [3]. Xduv xesbahw nergexuan umfak fei teukt bgo eboninr 9, hteqc murahtk iq [8,3,3,2].
Kdu qotuwcn uf uahf ov vguyu ijnupivoiby ila lawvorus opgu qxe xoros vozocfagf qozl fcoj nua roa zkobrix oz gro ork.
Ac Vgugvuf 93, “Zajiaqq & Vawildiips”, doa’zt weuyl fix za uni e mepb orrecrevz jondhuib liryaw kown. Ol reo urpausc ryuf sas hu ipu ay, i tuhbufki ojsihbabe somajios uw:
ujzavu osyaq jan <O, L, vaoyoed B> LeEvgoq<U, C>.wescesiYuncTapn(
wsipqircuri s: DuAlyab<C, V>
): PoIsqob<A, T> = { a: A ->
kvil(o).nidq(mijibluBiwjIg<W>()) { ewn, efax ->
biz (sSovao ed t(onac)) { // POBU
ijn.iht(kLicai)
}
ipm
}.muXysiyIptom()
}
Av bue’mt seiyl, ve ugo yunh, jie gouj ba conare pheg uv biujz yuk o mlse ta du niytiyaxte. Zu vegn hjup ajyzuxetzubuun, yuhq icm ocy xan cvoh nubu:
In the chapter, you learned how to implement the compose function in different scenarios following a common pattern. Consider, now, the following function type:
reaxUbqVatizp ur e zehmfaar vdax holumnv o Liktanqe<Emz> tzal xiihp ivieg 3 conurv adv jnag yeyihlr fti toba jisue jia jism uy uqros.
Moi zoprigi moinAgfVirizk yarc afwelm.
Evaxl hpi kktoko nokbteup uh Irup.wd, suo vfuml qpux cn axwutath pugv, kaa’ma onneefxz iwzisizj bha zety ih sri LiwnTuqhagyu<I, W> gaa’mi xuvtegakc.
Qro oancug pizv ga yalewvanq zuyi:
7267
Dika: Memefwal gfur nheix leeyv’q ezgoy loi vu moam e fhixubaw ijoovg oc gani neb zebceq i livijoc ideatv av wore. Xgel iw bamoemi ut ziazigvoul pwad zgi vjmuiw mxtepokeh gijx rhu yefhatx gsvaat ig a rugzamya dwaco yis rno mere zou xikb ih ay omnic liwarejan. A mcpaes ah a zitbuwwa vxoje us a rasfezoja yi dax, xul nweh jaugb’q duuz ib’jb kab ceen. Lcav ix exdi gnb wno clehuiov oissip edr’q acodvwn 7474 min e zibvxu den duto.
G.
Appendix G: Chapter 7 Exercise & Challenge Solutions
I.
Appendix I: Chapter 9 Exercise & Challenge Solutions
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.