Adding more tests, fixing one test for miniImp, adding more functionality to miniFun
This commit is contained in:
@ -9,7 +9,7 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 1)
|
||||
Printf.printf "Identity program: %d\n" (reduce program 1)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Constant program *)
|
||||
@ -20,7 +20,7 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 10)
|
||||
Printf.printf "Constant program: %d\n" (reduce program 10)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Partial application of function program *)
|
||||
@ -32,7 +32,7 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 2)
|
||||
Printf.printf "Partial application of function program: %d\n" (reduce program 2)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Partial application of function program *)
|
||||
@ -45,7 +45,7 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 3)
|
||||
Printf.printf "Partial application of function program: %d\n" (reduce program 3)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Passing functions to functions program *)
|
||||
@ -78,8 +78,8 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program (3));
|
||||
Printf.printf "%d\n" (reduce program (-3))
|
||||
Printf.printf "Passing functions to functions program 1: %d\n" (reduce program (3));
|
||||
Printf.printf "Passing functions to functions program 2: %d\n" (reduce program (-3))
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Recursive function program *)
|
||||
@ -92,7 +92,7 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 10)
|
||||
Printf.printf "Recursive function program: %d\n" (reduce program 10)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Scope program *)
|
||||
@ -104,4 +104,93 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce program 4)
|
||||
Printf.printf "Scope program: %d\n" (reduce program 4)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Factorial program *)
|
||||
let program =
|
||||
LetFun (
|
||||
"f",
|
||||
["x"],
|
||||
(IfThenElse (CmpLessEq (Variable "x", Integer 0), Integer 1, Times (Variable "x", Application (Variable "f", [Minus (Variable "x", Integer 1)])))),
|
||||
(Variable "f")
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "Factorial program: %d\n" (reduce program 10)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Hailstone sequence's lenght program *)
|
||||
|
||||
let program =
|
||||
LetFun (
|
||||
"collatz",
|
||||
["n"; "count"],
|
||||
(
|
||||
IfThenElse (BNot (Cmp (Variable "n", Integer 1)),
|
||||
(IfThenElse (Cmp (Modulo (Variable "n", Integer 2), Integer 0),
|
||||
Application (Variable "collatz", [Division (Variable "n", Integer 2); Plus (Integer 1, Variable "count")]),
|
||||
Application (Variable "collatz", [(Plus (Integer 1, Times (Integer 3, Variable "n"))); Plus (Integer 1, Variable "count")]))),
|
||||
(Variable "count"))
|
||||
),
|
||||
(Function (["x"], Application (Variable "collatz", [Variable "x"; Integer 1])))
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "Hailstone sequence's lenght program: %d\n" (reduce program 77031)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Sum multiples of 3 and 5 program *)
|
||||
|
||||
let program =
|
||||
LetFun (
|
||||
"sum",
|
||||
["n"],
|
||||
(IfThenElse ((BOr (Cmp (Modulo (Variable "n", Integer 3), Integer 0), Cmp (Modulo (Variable "n", Integer 5), Integer 0))),
|
||||
Plus (Variable "n", Application (Variable "sum", [Minus (Variable "n", Integer 1)])),
|
||||
(IfThenElse ((CmpLessEq (Variable "n", Integer 1)),
|
||||
(Integer 0),
|
||||
(Application (Variable "sum", [Minus (Variable "n", Integer 1)])))
|
||||
))
|
||||
),
|
||||
(Variable "sum")
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "Sum multiples of 3 and 5 program: %d\n" (reduce program 12345)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Rand program *)
|
||||
let program =
|
||||
Function (
|
||||
["x"],
|
||||
Rand (Variable "x")
|
||||
)
|
||||
|
||||
;;
|
||||
|
||||
Printf.printf "Rand program: %b\n" ((reduce program 10) < 10)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Fibonacci program *)
|
||||
let program =
|
||||
LetFun (
|
||||
"fib",
|
||||
["i"; "a"; "b"],
|
||||
(IfThenElse (Cmp (Variable "i", Integer 0),
|
||||
Variable "a",
|
||||
Application (Variable "fib", [Minus (Variable "i", Integer 1);
|
||||
Variable "b";
|
||||
Plus (Variable "a", Variable "b")])
|
||||
)),
|
||||
Function (["x"], (Application (Variable "fib", [Variable "x"; Integer 0; Integer 1])))
|
||||
)
|
||||
|
||||
;;
|
||||
|
||||
Printf.printf "Fibonacci program: %d\n" (reduce program 48)
|
||||
;;
|
||||
|
||||
Reference in New Issue
Block a user