Chapters

Hide chapters

Functional Programming in Kotlin by Tutorials

First Edition · Android 12 · Kotlin 1.6 · IntelliJ IDEA 2022

Section I: Functional Programming Fundamentals

Section 1: 8 chapters
Show chapters Hide chapters

Appendix

Section 4: 13 chapters
Show chapters Hide chapters

J. Appendix J: Chapter 10 Exercise Solutions
Written by Massimo Carli

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

This is because:

  • UByte is an unsigned 8-bit integer with ranges from 0 to 255.
  • Boolean can be true or false.
  • Unit represents a singleton.

The total number of possible values you can represent with Triplet is then 512.

Exercise 10.2

What’s the cardinality of the following type?

typealias Unique = Pair<Unit, Unit>

Is this isomorphic with 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>>

Is MultiEither isomorphic with MultiEither2, which you define in the following way?

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

Repeat the exercise with MultiEither2, and you’ll get:

(UByte + Boolean) + Triage = (256 + 2) + 3 = 256 + 5 = 261

They’re the same because addition is associative. You can find a function that maps each value in MultiEither to each value in MultiEither2 and vice versa. Because of this, the two types are isomorphic.

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.
© 2026 Kodeco Inc.