Question: 1 B ( 8 points ) : Define Closure for Function Calls with Zero or More Args. We will now redefine closures for functions with

1B (8 points): Define Closure for Function Calls with Zero or More Args.
We will now redefine closures for functions with zero or more args. When our function had one argument, our closures were defined as Closure(id, expr, env). We would now like to define closures as Closure([id1,..., idn], expr, env) where
- id1..., idn are the list of arguments for the function to be called.
- expr is the body of the function and
- env is the stored environment for static scoping.
\[
\begin{aligned}
\text { Value } & \Rightarrow \text { num(Double)}\\
& \Rightarrow \text { closure(String }{}^{*},\text { Expr, Environment)}\\
& \Rightarrow \text { error }
\end{aligned}
\]
For Environment please use a scala immutable map from String to Value
```
sealed trait value
case class Num(d: Double) extends Value
case object Error extends Value
// YOUR CODE HERE
case class Closure(params: List[String], expr: Expr, env: Map[String, Value]) extends Value
```
defined trait Value
defined class Num
defined object Error
defined class Closure
//BEGIN TEST
val v1= Closure(List("x","y","z"), Plus(x, y), Map.empty)
val v2= Closure(Nil, Const(15), Map.empty)
val env1: Map[String, value]= Map("x"-> Num(25.0),"y"-> v2)
val v3= Closure(List("x"), Plus(x, Const(25)), env1)
passed(8)
//END TEST
cmd21.sc:2: type mismatch;
found : ammonite.\$sess.cmd8.wrapper.cmd6.Ident
required: string
val v1= Closure(List("x","y","z"), Plus(x, y), Map.empty)
cmd21.sc:2: type mismatch;
found : ammonite.\$sess.cmd7.wrapper.cmd6.Ident
required: string
val v1= Closure(List("x","y","z"), Plus(x, y), Map.empty)
cmd21.sc:5: type mismatch;
found : ammonite.\$sess.cmd8.wrapper.cmd6.Ident
required: string
val v3= Closure(List("x"), Plus(x, Const(25)), env1)
cmd21.sc:5: type mismatch;
found : cmd21.this.cmd18. Const
required: string
val v3= Closure(List("x"), Plus(x, Const(25)), env1)
Compilation Failed
1 B ( 8 points ) : Define Closure for Function

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 Programming Questions!