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

G. Appendix G: Chapter 7 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 7.1

Implement the extension function isEmpty(), which returns true if FList<T> is empty and false otherwise.

Exercise 7.1 solution

You can solve this exercise a few different ways. The first uses what you implemented in the chapter and the size function.

Exercise 7.2

Implement the extension function tail(), which returns the tail of a given FList<T>.

Exercise 7.2 solution

You can easily implement the tail function like this:

Exercise 7.3

Kotlin provides forEachIndexed for the Iterable<T> interface, which accepts as input a lambda of type (Int, T) -> Unit. The first Int parameter is the index of the item T in the collection. To test forEachIndexed, run the code:

Exercise 7.3 solution

The solution, in this case, is a little more complicated because you need to keep track of the index for the current element. The following implementation is a possible option:

Exercise 7.4

Another option to implement forEachIndexed is to make FList<T> an Iterable<T>. How would you do that? To make all the code coexist in the same codebase, call the Iterable<T> version IFList<T> with INil and ICons<T>.

Exercise 7.4 solution

As mentioned in the problem statement, start the exercise with the existing FList<T> definition in FList.kt and rename it in IFList<T> along with INil and ICons<T>. A possible solution could be:

Exercise 7.5

Implement addHead, which adds a new element at the head of an existing FList<T>.

Exercise 7.5 solution

The addHead function is very simple:

Exercise 7.6

Kotlin defines the take function on Iterable<T> that allows you to keep a given number of elements.

Exercise 7.6 solution

A possible implementation of the take function for FList<T> is the following:

Exercise 7.7

Kotlin defines the takeLast function on Iterable<T> that allows you to keep a given number of elements at the end of the collection. For instance, running the following code:

Exercise 7.7 solution

This exercise is a little more complicated than the previous one. A first implementation could be the following:

Challenge 7.1

Kotlin provides the functions first and last as extension functions of List<T>, providing, if available, the first and last elements. Can you implement the same for FList<T>?

Challenge 7.1 solution

The implementation of first for FList<T> is simple because it’s exactly the same as head, which you implemented in the chapter.

Challenge 7.2

Kotlin provides an overload of first for Iterable<T> that provides the first element that evaluates a given Predicate<T> as true. It also provides an overload of last for List<T> that provides the last element that evaluates a given Predicate<T> as true. Can you implement firstWhen and lastWhen for FList<T> with the same behavior?

Challenge 7.2 solution

A very simple first implementation for firstWhen is:

Challenge 7.3

Implement the function get that returns the element at a given position i in FList<T>. For instance, with this code:

Challenge 7.3 solution

Creating a set of reusable functions is a very powerful tool and allows you to implement a complete library. A possible solution for the get function is the following:

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