ocaml: phase 5.1 expr_eval.ml baseline (9/9 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
A tiny arithmetic-expression evaluator using: type expr = Lit of int | Add of expr*expr | Mul of expr*expr | Neg of expr let rec eval e = match e with | Lit n -> n | Add (a,b) -> ... Exercises type-decl + multi-arg ctor + recursive match end-to-end. Per-program timeout in run.sh bumped to 120s.
This commit is contained in:
19
lib/ocaml/baseline/expr_eval.ml
Normal file
19
lib/ocaml/baseline/expr_eval.ml
Normal file
@@ -0,0 +1,19 @@
|
||||
(* Baseline: a tiny expression evaluator using ADTs + match *)
|
||||
type expr =
|
||||
| Lit of int
|
||||
| Add of expr * expr
|
||||
| Mul of expr * expr
|
||||
| Neg of expr
|
||||
;;
|
||||
|
||||
let rec eval e =
|
||||
match e with
|
||||
| Lit n -> n
|
||||
| Add (a, b) -> eval a + eval b
|
||||
| Mul (a, b) -> eval a * eval b
|
||||
| Neg x -> 0 - eval x
|
||||
;;
|
||||
|
||||
(* (1 + 2) * (3 + 4) - 5 = 21 - 5 = 16 *)
|
||||
eval
|
||||
(Add (Mul (Add (Lit 1, Lit 2), Add (Lit 3, Lit 4)), Neg (Lit 5)))
|
||||
Reference in New Issue
Block a user