J.
Appendix J: Chapter 10 Exercise Solutions
Written by Massimo Carli
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Unlock now
Exercise 10.1
What’s the cardinality of the following type?
typealias Triplet = Triple<UByte, Boolean, Unit>
Exercise 10.1 solution
As you learned in the chapter, the cardinality of Triplet
is the product of the cardinalities of UByte
, Boolean
and Unit
, which are:
UByte * Boolean * Unit = 256 * 2 * 1 = 512
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Unlock now
Exercise 10.2
What’s the cardinality of the following type?
typealias Unique = Pair<Unit, Unit>
Exercise 10.2 solution
Of course, the cardinality of Unique
is exactly 1
because Unit
is the only existing value of type Unit
. Because of this, Unique
is isomorphic to Unit
.
Exercise 10.3
What’s the cardinality of the following type?
typealias MultiEither = Either<UByte, Either<Boolean, Triage>>
typealias MultiEither2 = Either<Either<UByte, Boolean>, Triage>
Exercise 10.3 solution
In the chapter, you learned that Either<A, B>
is a way to represent addition. For this reason, you can represent the previous definition like:
UByte + (Boolean + Triage) = 256 + (2 + 3) = 256 + 5 = 261
(UByte + Boolean) + Triage = (256 + 2) + 3 = 256 + 5 = 261