Haskell Kotlin

The Main Pillar Of Kotlin Lazy Evaluation Vs Haskell Laziness

Before the start of this article, I just wanna make sure that every programmer who reads this article must be lazy like me.

Lazy Programming

In most of the functional programming languages– value don’t need to compute if they are not going to be used in the first place. Miranda, Haskell, and many other programming languages have the concept of laziness which holds the evaluation of an expression until the value is actually needed. While on the other-hand some programming languages are strict in evaluation.

Kotlin Strict Evaluation Example

I’ll start with the example of strict evaluation.

fun main() {
    printValue(getValue())
}

private fun printValue(value : Int) = println("Nothing")

private fun getValue() : Int {
    println("Returning 5")
    return 5
}

In Kotlin, as soon as the program run, the getValue function is called to provide the corresponding value, although the return value will never be used in the printValue method. This behavior is because of Kotlin eager evaluation and the Applicative-Order of calling function. In applicative-order language, the function first evaluates all of the arguments completely, then pass them to the function as a parameter.

We can override the above behavior of Kotlin programming language and make it lazy in evaluation but before that, I just wanna show you something interesting.

Haskell Lazy Evaluation Example

Now if we take the above example a little further and write the same code in a pure lazy programming language like Haskell. The getValue method will never be evaluated. Let’s see an example of what I’m talking about.

import Debug.Trace

getValue = trace ("Returning 5") 5  // 1

printValue value = print("Nothing")  // 2

main = printValue (getValue)  // 3

I did not have the plugin for Haskell programming language to run the code online. So, I’m going to show you the output of the above program below.

Haskell program output

If you look at the second line of the above output you’ll notice that the output section is only showing Nothing and getValue method is not being called. Well, that is because Haskell uses the lazy evaluation with Normal-Order of calling function. In normal-order, you apply the function before you evaluate its argument.

So, the main reason for the getValue method never gets called is because when the printValue method evaluates it simply prints to console Nothing. The printValue method never actually use the value argument. Then Haskell says why to bother, I’m not going to spend my effort running on the getValue method.

Essentially Haskell don’t evaluate stuff unless otherwise the program really need an evaluation.

Kotlin Lazy Evaluation EXAMPLE

Let’s get back to our previous Kotlin code and try to make it lazier.

fun main() {
    printValue(getValue())
}

private fun printValue(value: Lazy<Int>) = println("Nothing")

private fun getValue() = lazy {
    println("Returning 5")
    [email protected] 5
}

Now if you press the run button, you’ll see the Kotlin did not evaluate the getValue method because I wrap the method with by lazy construct. This is very similar to what you can do in F# and Scala as well. You add the lazy keyword before val and tells the compiler, “this one you can lazy about it”. Even if it has side-effects I’m not going to offend if you don’t call straight away. So, the language compiler says alright alright–I left upon the eagerness here and postpone the evaluation until you need it.

Conclusion

Some languages are born lazy like they were sitting on the beach and doing nothing and some languages to work really hard to be lazy. Kotlin by default is a strict language but in order to perform laziness, we’ve to tell the language that this where I need you to postpone the evaluation until I need it.

What’s next

Next, we’ll see the difference between Kotlin by lazy and lateinit delegate property. Stay tuned!

If you like this article please click the ♥️ button and shares to help others find it. Feel free to leave a comment.

Thank you for being here and keep reading…

Write A Comment