Question: Question 9 : Consider the Scala definitions: enum Exp: case Val ( n:Int ) case Plus ( e 1 :Exp, e 2 :Exp ) case

Question 9: Consider the Scala definitions:
enum Exp:
case Val (n:Int)
case Plus (e1:Exp, e2:Exp)
case Times (e1:Exp, e2:Exp)
def f (e:Exp) : Int = e match {
case Val (v)=> v
case Plus (e1, e2)=> f (e1)+ f (e2)
case Times (e1, e2)=> f (e1)* f (e2)
}
With the above definitions, what is the outcome of running the following Scala code?
val sample = Times (Plus (Val (1), Val (2)), Val (3+4))
println (f (sample))
A : The code runs successfully and prints: 21
B : The code fails to compile because the parameter to Val is called n in the class declaration, but is called v in the
function f.
C : The code fails to compile because the parameter to Val is not a single Int but an expression 3+4.
D : The code runs successfully and prints: 11
E : The code runs successfully and prints: 10

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!