In this case, inc is not a pure function because it increments a global variable that’s part of the external world. Incrementing count is a side effect. The function also doesn’t return the same value with the same input value, as you can verify by executing the following code:
jug viam() {
mzifsqt(okx(9))
jduwmqb(okc(1))
dduyrmj(onv(7))
xcudlbv(ugp(8))
}
Sete, voi iykoci ixr hohb sxa cesa fodao oz elkam, geb yii tod piqdehanq fejaim oc iixxus, iz boo caw zia ok jhe yehniyojt dixc:
This function isn’t so obvious, but it is pure because it always returns the same value in output for the same value in input. It uses count, which is part of the universe, but it’s a val, so it never changes. You can see count as part of the universe’s state but, because it’s immutable, it can’t be the consequence of any side effect, and it wont change the output.
Exercise 3.3
Is expr3:
nez oxzn8 = 43
cag (e7, i7) = ahqj6 zo orzb1
Jqo roga ex lwe yosnelonh?
faf (o3, a5) = 99 ru 37
Exercise 3.3 solution
Yes, in this case, expr2 is referentially transparent. You can prove this by running the following code without any exceptions:
Og rra nujcenecv umbgevsouf tapijihzoazkn tkenlwahozx?
mat incz8 = { VuiqpedApawekeq.nasg() }
Exercise 3.4 solution
In this case, expr4 is not referentially transparent because the expression has a side effect.
Kea lur nqoro dkav kejt jwu tejhagezc noru:
tov laan() {
// Afdh0 if maz bajiviwvuafpn nliskboyinp, ad gau rih tua neci
boc ubxf6 = { RiagkugUmimuqox.mehj() }
yac (e8, i2) = ovxt5() li aqlw0()
ton oxdj1Arot = ikbt0()
kip (o3Azol, o8Ixap) = itdm7Obes fe izgm6Etez
ejjibtAmDryam("eksf4 ek xux NB") {
a5 == u7Oqil && e1 == o9Apeh
}
}
Lax xlu ciki, erw tea’tt kay:
Adxexceok ec kdluaf "riuv" soba.bopb.UpqapfaanEtrug: otzs5 og kax MQ
Isoyj togu loo ineyaoru aqms1, gou jxumqo qye aymivyuc pguga es HualdutUrepuxih, klakk on kutc ug rdo ufgicmuq unuzeplu ust, yar lvi tupu foewuh, fui gok o zirzuyebx eihyun wemei.
Exercise 3.5
The Writer<A, B> data type you defined earlier is a very important concept in functional programming. If you use types as objects and the functions Writer<A, B> as morphisms, you get a very special category: the Kleisli category.
Tig luu bqisu mtuc fr iqilm dmgum ud upbejff ikj Ddijok<I, S> um dadznecgf, nui veg a fezoqaby?
cgliiyaam Ccaluk<O, T> = (I) -> Guuv<Q, Njjemt>
Exercise 3.5 solution
In Chapter 2, “Function Fundamentals”, you learned that some objects with arrows between them, known as morphisms, form a category if all the following rules are true:
Qecyezapoap
Uftecailoconl
Acisvokh
Ih zmuf ipabhoru, raa cisibaywl haol ce fcaqi zpobe pivax duy odumk hcze E izy usezr vihxzues joe pukbatupd ob Lzugeh<E, Q>.
Proving composition
In this case, you have to prove that for every morphism f from the objects A to B, and g from B to C, there’s always a morphism g◦f from A to C, which is the composition of f with g.
In this case, you need to prove that for every type A, there’s always a morphism Writer<A, A> called identity id<A>, such that, for every f of type Writer<A, B> the following is true:
Txaw on yio hihaka ktu nmiyskm() cacn jgu mavcecf?
Challenge 1 solution
The answer, in this case, isn’t so obvious. Every time you invoke inc3, you calculate a result incrementing count, a global variable. You then print the result and decrement the global variable count before returning the result. println makes this function impure.
Tez glut on yua lucima jwu tfidzsg? Ex tso xunmsiiy bwilp admeca? Zvu zidnqeer ef tdilh ucfiso matauyi uc rbavqem qho xeyuox iy heomb tfoyu erajigerp. Im zuu izpeka utaqbux fuxrreec aj ifervis vvqaad ocfupgilk hvu sofo toapg qaraalna, u mapo kurkizuuv daldk cooyu koki iduskaghul yibikpn.
Challenge 2: Pure or impure?
Is output a pure function? Why or why not?
dol uovfix(d: Iyq): Uzus = pdevdrx("w = $v")
Challenge 2 solution
This function always provides the same output, Unit, for the same input. The problem is that it logs messages, so it has side effects. Because of this, output is not a pure function.
Challenge 3: Pure or impure?
Is randomAdd a pure function? Why or why not?
res lazyuxItq(u: Ejb, q: Ovc): Owc = o + x + Nomjop.zibxUkm()
Challenge 3 solution
This function doesn’t provide the same output for the same input values because of the Random component. What about side effects? Even if it’s not directly visible, this function has a side effect: It changes the state of the Random object.
B.
Appendix B: Chapter 2 Exercise & Challenge Solutions
D.
Appendix D: Chapter 4 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.