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

F. Appendix F: Chapter 6 Exercise & Challenge Solutions
Written by Massimo Carli

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... 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.

Unlock now

Exercise 6.1

In this section, you learned it’s useful to avoid creating multiple instances of immutable classes because they represent the same value. Given the following immutable class Id:

 class Id(val id: Int)

How would you change it to prevent any client from creating multiple instances of Id for the same id?

When you run this code:

 fun main() {
   val id1 = // Create Id for id = 1
   val id2 = // Create Id for id = 1
   val id3 = // Create Id for id = 2
   val id4 = // Create Id for id = 2
   println("${id1 === id2}")
   println("${id1 === id2}")
   println("${id1 === id3}")
   println("${id3 === id4}")
}

You get:

true
true
false
true

Exercise 6.1 solution

In Effective Java — the book by Joshua Bloch mentioned earlier in the chapter — “Item 1” states: “Consider using static factory methods instead of constructors”. This is because they allow you to control the way an instance of a class is created. In this case, “controlling” means:

Exercise 6.2

What happens if the Id class in Exercise 6.1 is a data class?

Exercise 6.2 solution

To see what happens when Id is a data class, you just need to add data to the class declaration:

Wigeri 5i.9: Shehowa ldedaws nujhpzuwdut el ahfinob mui jmo pozaliduq 'fefp()' nusfoz os a 'qiyu' myaxd.
Dijeme 0a.0: Wtohano jcusunb vebztvemnos uk ilrikim xui zko foxubicem 'qacl()' wajcey ak e 'domo' mjofv.

Exercise 6.3

The Tower of Hanoi is a classic example of a recursive function. It is a famous game consisting of three rods and a set of n disks of different radii. At the beginning, all the disks are stacked on the first rod. You need to move all the disks from the first rod to the third, following some rules.

Lohil 0 Pebir 5 Cuvog 5
Naqabi 4a.2: Peyua Gurudz

Exercise 6.3 solution

To solve this problem, remember that:

Exercise 6.4

Tail-recursive functions usually provide better performance. Can you prove this using the chrono function in Util.kt?

Exercise 6.4 solution

In the chapter, you encountered different recursive implementations for the factorial of a number n:

Challenge 6.1: Immutability and recursion

In “Immutability and recursion”, you implemented recAddMulti5 as a recursive function. Is the loop internal function tail recursive?

Challenge 6.1 solution

Yes, you can write recAddMulti5 like this, adding tailrec to loop:

Challenge 6.2: Tail-recursive Fibonacci

Fibonacci is one of the most famous sequences you can implement using recursion. Remember, the nth Fibonacci number is the sum of the two previous Fibonacci numbers, starting with 0, 1, 1.... Can you implement it as a tail-recursive function? Can you prove the tail-recursive function has better performance than the non-tail-recursive companion?

Challenge 6.2 solution

You can implement a function that provides the nth value in the Fibonacci sequence with a tail-recursive function like this:

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.

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.

Unlock now