Related questions
in Ocaml, implement the next part of function
let rec eval_ession env e = (**)
up to the designated point
You may use the included functions, the Stdlib module, the Str module, the List module, and the Re module. NO IMPERATIVES. NO REFERENCES. NO HASHMAPS.
List of each kind of ession:
type ession =
| Int of int
| Bool of bool
| String of string
| ID of var
| Not of ession
(*Continue from Here*)
| Binop of op * ession * ession
(* Stop here: rest of the function will be asked in followup questions *)
| If of ession * ession * ession
| App of ession * ession
| Let of var * bool * ession * ession
| Fun of var * ession
| Closure of environment * var * ession
| Record of (label * ession) list
| Select of label * ession
type op =
| Add
| Sub
| Mult
| Div
| Concat
| Greater
| Less
| GreaterEqual
| LessEqual
| Equal
| NotEqual
| Or
| And
FUNCTIONS SO FAR
(* Exceptions *)
exception TypeError of string
exception DeclareError of string
exception SelectError of string
exception DivByZeroError
(* Functions to manage environments *)
let extend env x v = (x, ref v) :: env
let rec lookup env x = match env with | [] -> raise (DeclareError ("Unbound variable " ^ x)) | (var, value) :: t -> if x = var then !value else lookup t x
let extend_tmp env x = (x, ref (Int 0)) :: env
let rec update env x v = match env with | [] -> raise (DeclareError ("Unbound variable " ^ x)) | (var, value) :: t -> if x = var then value := v else update t x v
(* SO FAR *)
let rec eval_ession env e =
match e with
| Int _ | Bool _ | String _ -> e (* Base cases: these evaluate to themselves *)
| ID x -> lookup env x (* Lookup variable in the environment *)
| Not e1 -> (match eval_ession env e1 with | Bool b -> Bool (not b) | _ -> raise (TypeError "Expected type bool"))
(* CONTINUE FROM HERE *)
Binop
There are five sorts of binary operator: Those carrying out integer arithmetic; those carrying out integer ordering comparisons; one carrying out string concatenation; and one carrying out equality (and inequality) comparisons; and those implementing boolean logic.
Add, Sub, Mult, and Div
Arithmetic operators work on integers; if either argument evaluates to a non-Int, a TypeError should be raised. An attempt to divide by zero should raise a DivByZeroError exceptio.
eval_ession [] (Binop (Add, (Int 1), (Int 2))) = Int 3
eval_ession [] (Binop (Add, (Int 1), (Bool false))) (* TypeError "Expected type int" *)
eval_ession [] (Binop (Div, (Int 1), (Int 0))) (* DivByZeroError *)
Greater, Less, GreaterEqual, and LessEqual
These relational operators operate only on integers and produce a Bool containing the result of the operation. If either argument evaluates to a non-Int, a TypeError should be raised.
eval_ession [] (Binop(Greater, (Int 1), (Int 2))) = Bool false
eval_ession [] (Binop(LessEqual, (Bool false), (Bool true))) (* TypeError "Expected type int" *)
Concat
This operation returns the result of concatenating two strings; if either argument evaluates to a non-String, a TypeError should be raised.
eval_ession [] (Binop (Concat, (Int 1), (Int 2))) (* TypeError "Expected type string" *)
eval_ession [] (Binop (Concat, (String "hello "), (String "ocaml"))) = String "hello ocaml"
Equal and NotEqual
The equality operators require both arguments to be of the same type. The operators produce a Bool containing the result of the operation. If the two arguments to these operators do not evaluate to the same type (e.g., one boolean and one integer), a TypeError should be raised. Moreover, we cannot compare two closures for equality -- to do so risks an infinite loop because of the way recursive functions are implemented; trying to compare them also raises TypeError (OCaml does the same thing in its implementation, BTW).
eval_ession [] (Binop(NotEqual, (Int 1), (Int 2))) = Bool true
eval_ession [] (Binop(Equal, (Bool false), (Bool true))) = Bool false
eval_ession [] (Binop(Equal, (String "hi"), (String "hi"))) = Bool true
eval_ession [] (Binop(NotEqual, (Int 1), (Bool false))) (* TypeError "Cannot compare types" *)
Or and And
These logical operations operate only on booleans and produce a Bool result. If either argument evaluates to a non-Bool, a TypeError should be raised.
eval_ession [] (Binop(Or, (Int 1), (Int 2))) (* TypeError "Expected type bool" *)
eval_ession [] (Binop(Or, (Bool false), (Bool true))) = Bool true
Step by stepSolved in 2 steps
- Please do this in C++. The only parts needed are commented as //TODO in MAIN. Please do not use cout, the PrintNodeData in MileageTrackerNode.cpp needs to be called. I am struggling with adding data into the linked list. Given the MileageTrackerNode class, complete main() to insert nodes into a linked list (using the InsertAfter() function). The first user-input value is the number of nodes in the linked list. Use the PrintNodeData() function to print the entire linked list. DO NOT print the dummy head node. Ex. If the input is: 3 2.2 7/2/18 3.2 7/7/18 4.5 7/16/18 the output is: 2.2, 7/2/18 3.2, 7/7/18 4.5, 7/16/18 Main.cpp #include "MileageTrackerNode.h"#include <string>#include <iostream>using namespace std; int main (int argc, char* argv[]) {// References for MileageTrackerNode objectsMileageTrackerNode* headNode;MileageTrackerNode* currNode;MileageTrackerNode* lastNode; double miles;string date;int i; // Front of nodes listheadNode = new...arrow_forwardIn writing a general-purpose sort method in Java, the sort method needs to compareobjects. There are two ways to give the sort method the code to compare objects,implementing the interfaces Comparable and Comparator respectively. Extend Article class toimplement Comparable interface so that the default sorting of the Articles is by its volumearrow_forwardC++ Create a generic function increment(start, stop, x) that adds x to every element in the range [start,stop). The addition is done using the + operator. The arguments start and stop are bidirectional iterators. Write a test driver.arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education