concept implicit memoization in category kotlin
appears as: implicit memoization

This is an excerpt from Manning's book The Joy of Kotlin.
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.