Question from the Kotlin test

Write a Kotlin program that evaluates an arithmetic expression.

Hard

Considering this code :

sealed class Expr
data class Const(val number: Double) : Expr()
data class Sum(val e1: Expr, val e2: Expr) : Expr()
object NotANumber : Expr()

fun eval(expr: Expr): Double = when(expr) {
    is Const -> expr.number
    is Sum -> eval(expr.e1) + eval(expr.e2)
    NotANumber -> Double.NaN
}

fun main(args: Array<String>) {
    println(eval(Sum(Sum(Const(42.0), Const(1.0)), Const(1.0))))
}
Author: W3D TeamStatus: PublishedQuestion passed 532 times
Edit
3
Community EvaluationsNo one has reviewed this question yet, be the first!