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

D. Appendix D: Chapter 4 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 4.1

Can you write a lambda expression that calculates the distance between two points given their coordinates, x1, y1 and x2, y2? The formula for the distance between two points is distance = √(x2−x1)²+(y2−y1)².

Exercise 4.1 solution

You can approach this problem in different ways. Assuming you pass all the coordinates as distinct parameters, you can write the following code:

Exercise 4.2

What’s the type for the lambda expression you wrote in Exercise 4.1?

Exercise 4.2 solution

The type of distanceLambda is:

Exercise 4.3

What are the types of the following lambda expressions?

Exercise 4.3 solution

You start by looking at:

Nothing and lambda

Given the type:

Exercise 4.4

Can you implement a function simulating the short-circuit and operator with the following signature without using &&? In other words, can you replicate the short-circuiting behavior of left && right:

Exercise 4.4 solution

In Kotlin, if is an expression, so you can implement shortCircuitAnd like this:

Exercise 4.5

Can you implement the function myLazy with the following signature, which allows you to pass in a lambda expression and execute it just once?

Exercise 4.5 solution

myLazy accepts a lambda expression of type () -> A as an input parameter. It’s also important to note that A has a constraint, which makes it non-null. This makes the exercise a little bit easier, because you can write something like:

Exercise 4.6

Create a function fibo returning the values of a Fibonacci sequence. Remember, every value in a Fibonacci sequence is the sum of the previous two elements. The first two elements are 0 and 1. The first values are, then:

Exercise 4.6 solution

The following is a possible implementation for the Fibonacci sequence using lambda evaluation:

Challenge 4.1

In Exercise 4.5, you created myLazy, which allowed you to implement memoization for a generic lambda expression of type ()-> A. Can you now create myNullableLazy supporting optional types with the following signature?

Challenge 4.1 solution

To remove the constraint, you just need to use an additional variable, like this:

Challenge 4.2

You might be aware of Euler’s number e. It’s a mathematical constant of huge importance that you can calculate in very different ways. It’s an irrational number like pi that can’t be represented in the form n/m. Here you’re not required to know what it is, but you can use the following formula:

Noqahu 2.6: Iahob'n peklati
Kalezo 9.5: Uujed'j vozwazo

Challenge 4.2 solution

A possible implementation of the Euler formula is:

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