Question: Problem 4 ( a ) : Cons Operator and ENil Expression Modify the evalOp function to handle the Cons operator. The Cons operator takes two
Problem a: Cons Operator and ENil Expression Modify the evalOp function to handle the Cons operator. The Cons operator takes two values and returns a pair a VPair value of the two values. evalOp Cons VInt VInt : Also, update the eval function to handle the ENil expression. ENil should evaluate to VNil. eval ENil Problem b: head and tail Functions Finally, let us add support for two functions head and tail that operate on lists. These two functions are different from regular functions in that they are not defined in the Nano program. This is because they need to operate on values directly, rather than expressions. To handle this, we will define them as primitive functions and add them to the environment when evaluating expressions. VPrim is used to represent primitive functions. It takes a function of type Value Value that directly operates on values. When a primitive function is applied to a value, it will call the function with the value as an argument and return the result. Modify the eval function so that when a VPrim is applied to a value, the function is called on the value and returns the new value. Then, implement the head and tail functions and add them to prelude. The head function should take a pair and return the first element, and the tail function should take a pair and return the second element. If the value is not a pair, the function should throw a type error using throw Error "type error"or a customized error message, as long as the string "type error" is in there prelude is the default environment that is used when evaluating expressions. Add the head and tail functions to the prelude environment so they will be available when evaluating expressions. If head or tail is applied to a nonlist value or the empty list, the function should throw an error. Feel free to customize the error message as you see fit. Applying head or tail to an empty list would be an error, but not a type error. Applying them to a nonlist value would be a type error. let el EBin Cons EInt EBin Cons EInt ENil execExpr el : : execExpr EApp "head" el execExpr EApp "tail" el :
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
