Skip to content
Snippets Groups Projects
Commit 42afe0a0 authored by Paul Iannetta's avatar Paul Iannetta Committed by Thomas Wood
Browse files

update of test files

parent 516425a2
No related branches found
No related tags found
No related merge requests found
Stack = {
var is_empty = function (s) {
return s === Nil;
};
var push = function (x, stack) {
return {type: "Cons", value: x, stack: stack};
};
var pop = function (stack) {
return (function () {
switch (stack.type) {
case "Cons": var x = stack.value, xs = stack.stack;
return x;
case "Nil":
return stuck("Empty list");
}
}());
};
}
var eval_ = function (expr) {
return (function () {
switch (expr.type) {
case "Const": var n = expr.value;
return n;
case "Add": var ls = expr.left, rs = expr.right;
return eval_(ls) + eval_(rs);
case "Sub": var ls = expr.left, rs = expr.right;
return eval_(ls) - eval_(rs);
case "Mul": var ls = expr.left, rs = expr.right;
return eval_(ls) * eval_(rs);
case "Div": var ls = expr.left, rs = expr.right;
return eval_(ls) / eval_(rs);
}
}());
};
var print_expr = function (expr) {
return (function () {
switch (expr.type) {
case "Const": var n = expr.value;
return to_string(n);
case "Add": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " + " + print_expr(rs);
case "Sub": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " - " + print_expr(rs);
case "Mul": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " * " + print_expr(rs);
case "Div": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " / " + print_expr(rs);
}
}());
};
var f = (function () {
var source = parse("((1972 / 29) / 2) + 8");
return print(print_expr(source) + " = " + to_string(eval_(source)));
}());
......@@ -22,5 +22,6 @@ let rec print_expr expr = match expr with
| Div (ls, rs) -> "(" + (print_expr ls) + ")" + " / " + (print_expr rs)
let f =
let rec bli = Stack.C(1, Stack.N) in
let source = parse "((1972 / 29) / 2) + 8" in
print ((print_expr source) + " = " + to_string (eval_ source))
......@@ -32,10 +32,11 @@ let rec fold_right f l acc = match l with
let rev_map f l = fold_left (fun acc x -> Cons(f x, acc)) Nil l
let map f l = fold_right (fun acc x -> Cons(f x, acc)) l Nil
let rev l = fold_left (fun acc x -> Cons(x, acc)) Nil l
let length l = fold_left (fun acc x -> incr acc) 0 l
let rec range i j acc = if le i j then range (incr i) j (Cons (i, acc)) else acc
let rec range i j acc = if i <= j then range (incr i) j (Cons (i, acc)) else acc
(* Test *)
......@@ -43,11 +44,18 @@ let list0 = Nil
let list1 = range 0 1 Nil
let list2 = range 1 5 Nil
let sqr x = mul x x;;
length list0;;
length list1;;
length list2;;
map sqr list0;;
map sqr list1;;
map sqr list2;;
let sqr x = mul x x
let print_list l =
let rec aux acc l = match l with
| Nil -> acc
| Cons (x, xs) -> print xs; if xs === Nil then aux (x + acc) xs else aux (x + "," + acc) xs
in "[" + aux "" (rev l) + "]"
;;
let f = 1 in
print (length list0);
print (length list1);
print (length list2);
print (print_list (map sqr list0));
print (print_list (map sqr list1));
print (print_list (map sqr list2));
type 'a stack =
| Nil
| Cons [@f value, stack] of 'a * 'a stack
type stack =
| C [@f value, stack] of int * stack
| K [@f value, stack] of int * stack
| N [@f]
| B [@f]
let is_empty s = s === Nil
let is_empty s = s === N
let push x stack = Cons(x, stack)
let push x stack = C(x, stack)
let pop stack =
match stack with
| Cons (x, xs) -> x
| Nil -> stuck "Empty list"
| C (x, xs) -> x
| K (x, xs) -> x
| B -> stuck "Empty list"
| N -> stuck "Empty list"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment