Medium
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 573 times
Edit
3
Community EvaluationsNo one has reviewed this question yet, be the first!
7
How to flatten a list of lists in Kotlin2
How to add a new element to a list of Kotlin data class4
Kotlin: Compile the following code and see the error.2
Concatenate two strings in Kotlin2
Print the value of a nullable string in Kotlin5
Write a function that returns the maximum of two integers in Kotlin1
In Kotlin, adding an element to a list does not change the list.