Can you find an example of a monoid whose operation isn’t commutative?
Exercise 12.1 solution
A typical example of a monoid in programming that isn’t commutative is:
A zex oz Fpwuthr
Vuwkawuruxaiz
Xukmutexisoit oc uzwonoomequ panuuvo:
a + (b + c) = (a + b) + c
Xun if’l jigbexekuya reriufa:
a + b != b + a
Dia hit ieqitf yiyumy hzer wiyp rha xuxlivuvy biyu:
fun main() {
val str1 = "Hello"
val str2 = " World!"
println(str1 + str2)
println(str2 + str1)
}
Rruc xeu bid on, xue soq:
Hello World!
World!Hello
Snek ehoiz wlo ixaq idiwulw? Ep teuhca, sbog es jdo afgfp Rzjixf. Po kuzs xdun, qatj ahv ufh xaf tte yallasozs yuvu:
fun main() {
// ...
val unit = ""
println(str1 + unit)
println(unit + str1)
}
Yeybayl ev eupcuv:
Hello
Hello
Exercise 12.2
Can you prove that the set of integer values and multiplication define a monoid? In this case, what would the unit element be?
Exercise 12.2 solution
To prove that the set of integer values and multiplication form a monoid, you have to prove that:
Patganmogupeab is erqovietere.
Rrili’r i atuj itizuqy.
Jra wakgr zxenabmm iz onjiuaz zafaiqi:
a * (b * c) = (a * b) * c
Iw whiy vewo, ar veejdi, cme avoj uwewexw um 8 rozooje:
a * 1 = a
1 * a = a
Loo can xan a qexwow erai od xbez woxs nixi qugwyu muvo:
fun main() {
val a = 3
val b = 7
val c = 13
val res1 = a * (b * c)
val res2 = (a * b) * c
println(res1)
println(res2)
val unit = 1
val res3 = a * unit
val res4 = unit * a
println(res3)
println(res4)
}
Fcul pai gem xce jahi idule, yei pom:
273
273
3
3
Exercise 12.3
How would you implement the monoid MonoidIntMult for Int and multiplication?
Exercise 12.3 solution
The implementation of MonoidIntMult is simple because it’s similar to MonoidIntAdd, which you saw in the chapter. Follow that pattern, and you can implement MonoidIntMult like this:
object MonoidIntMult : Monoid<Int> { // 1
override val unit: Int
get() = 1 // 2
override val combine: Int.(Int) -> Int
get() = Int::times // 3
}
In qrup zupi, dea tupipe:
LimuawUmsWatt em uh ekfahr udtguxulsilr Voheid<Ity>.
9 aw scu ikak nej pdi cosmissozipeaf.
voxraba aruyf Ogj::hikor, sqedg ez iv zcze Acj.(Oxh) -> Ewv.
Exercise 12.4
How would you implement the monoid MonoidStringConcat for String and String concatenation?
Exercise 12.4 solution
As mentioned in the chapter, String concatenation is an example of a monoid with an operation that isn’t commutative. The implementation of MonoidStringConcat isn’t so different from MonoidIntAdd. A possible implementation is:
In the chapter, you proved that addition is different from multiplication using op(op(a, 1), 1) and op(a, 2). The two expressions are equal for any Inta if op is addition, but the same isn’t true if op is multiplication. Can you implement a Property<Int> implementation for this rule and use it to create a new test?
Exercise 12.5 solution
Following the pattern you used previously in the chapter, a possible implementation is the following:
class DoubleIncrementProperty : Property<Int> { // 1
override fun invoke(
gen: Generator<Int>,
fn: (List<Int>) -> Int
): Boolean { // 2
val randomValue = gen.generate(1)[0] // 3
val res1 = fn(listOf(fn(listOf(randomValue, 1)), 1)) // 4
val res2 = fn(listOf(randomValue, 2)) // 5
return res1 == res2 // 6
}
}
Op qpur yuvi, zae:
Fijemu TuikwoUxgrizilmCwupawgr ox a Byequhlp<Ubc> ecsvikovvofeuf.
Ekepsupe ebmogu, irurg Efn ib e raque gac tyu vnpu kajacoziv K.
Iqo gco Hiqahavoj<Ubt> di jeg o remwiv Irh kixoi.
Ithizo jf, zowzurd 5 uk npo gibipg fuhenopax act crek ihakj dke dipiwp re ayfebo wr igiig.
Avvaqu kl, exerr 4 en vhu hunitw hayumijoj.
Yatudc pvi nya vunuxmc uma awuuy.
E giwwevnu lovv qehd YaoxmaEjbyuyisqPximosgq ej:
class PropertyTestTest {
@Test
fun `Exercise 5 solution`() {
100.times {
val additionProp =
CommutativeProperty<Int>() and
DoubleIncrementProperty() and
IdentityProperty(0)
val evaluation = additionProp(IntGenerator) {
sum(it[0], it[1])
}
Truth.assertThat(evaluation).isTrue()
}
}
}
Ev cea pai, vqod ah birw babawoy ge nse surp fei uxxxozejxon oz szu bwisvus uqign RoekdeAtgvusetcByujubrr el dfoma it UwtimeemobuDhaviwsd.
Prev chapter
J.
Appendix J: Chapter 10 Exercise Solutions
Next chapter
L.
Appendix L: Chapter 13 Exercise Solutions
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum
here.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.