fr

Daily Challenge 🗓️

Your daily dose of technical intelligence to shine in (developer) society.

Discover a software development quiz per day with its detailed correction.

Frontend, Backend, Mobile, Data, Web, AI, Infra... Everything that can be coded!

Tuesday 19 August 2025

-

Kotlin

Kotlin #5/20

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 636 times
3
Reviews from developers
developer avatar
Auteur anonyme
01/02/2025
Formatter la partie eval() ``` eval( Sum( Sum(), Sum(), ) ) ```

About Daily Challenge 🗓️

From Monday to Friday, discover each day a quiz question with its detailed answer to enrich your tech knowledge. The questions are proposed by the WeLoveDevs.com community: experienced developers, trainers and freelancers.WeLoveDevs.com helps developers to find their dream job. We also provide over 50 free technical tests in different technologies for those of you who would like to test your knowledge.

History

    ⚠️
    Your browser is badly|not supported!
    We recommend you to use a more modern browser such as Edge, Chrome or Firefox
    Know More