concept exercise in category kotlin

appears as: exercises, exercises, The exercises, n exercise, exercise, exercise, The exercise
Functional Programming with Kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.

Appendix A provides a short primer to help those new to Kotlin come to grips with the language features. This primer will teach you all you need to know to understand the concepts and do the exercises in the book. Consider reading Manning’s Kotlin in Action for a more comprehensive treatment of the language itself.

This section contains all the solutions to the exercises in the book. Please make the best attempt possible at doing the exercises prior to skipping to this section to get the answers. The book is written in such a way that doing the exercises are a crucial part to your learning experience. Each exercise builds on the knowledge gained by the previous one. Please only use this section to verify your answers, or to help you if you are absolutely stuck.

We also know from exercise 11.12 that join is a flatMap combined with an identity function:

flatMap(join(y)) { it } ==
    flatMap(y) { join(it) }

join(join(y)) ==
    flatMap(y) { join(it) }

We have also learned in exercise 11.3 that flatMap can be expressed as a map and join, this eliminating the final flatMap.

The Joy of Kotlin

This is an excerpt from Manning's book The Joy of Kotlin.

Solutions follow each exercise, but you should first try to solve the exercise without looking at the answer. The solution code also appears on the book’s website. This exercise is simple, but some exercises will be quite hard, so it might be difficult to refrain from cheating. Remember that the harder you search, the more you learn.

Listing 4.1 Recursive Fibonacci with implicit memoization
fun fibo(number: Int): String {
    tailrec fun fibo(acc: List<BigInteger>,
                     acc1: BigInteger,
                     acc2: BigInteger, x: BigInteger): List<BigInteger> =
        when (x) {
            BigInteger.ZERO -> acc
            BigInteger.ONE -> acc + (acc1 + acc2)
            else -> fibo(acc + (acc1 + acc2), acc2, acc1 + acc2,    #1  
                          x - BigInteger.ONE)
        }
    val list = fibo(listOf(),    #2  
                    BigInteger.ONE, BigInteger.ZERO, BigInteger.valueOf(number.toLong()))
    return makeString(list, ", ")    #3  
}

fun <T> makeString(list: List<T>, separator: String): String =
    when {
        list.isEmpty() -> ""
        list.tail().isEmpty() -> list.head().toString()
        else -> list.head().toString() +    #4  
                 foldLeft(list.tail(), "") { x, y -> x + separator + y }
    }

#1   The first + sign on this line is the list concatenation operator; others represent BigIntegers addition.
#2   Calls the fibo helper function to get the list of Fibonacci numbers
#3   Formats the list into a comma-separated string through a call to makeString. This is only for an exercise. You could have used the standard Kotlin list.joinToString (", ") function instead.
#4   The + sign must be at the end of this line and not at the beginning of the next line; otherwise the code won’t compile.

Create a memoized version of the length function from exercise 3.8. Its signature in the List class is