aboutsummaryrefslogtreecommitdiff
path: root/comparison/OCaml
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--comparison/OCaml/Makefile36
-rw-r--r--comparison/OCaml/ack3_11.ml6
-rw-r--r--comparison/OCaml/bsort40000.ml23
-rw-r--r--comparison/OCaml/fib38.ml7
-rw-r--r--comparison/OCaml/isort40000.ml30
-rw-r--r--comparison/OCaml/msort800000.ml43
-rw-r--r--comparison/OCaml/qsort800000.ml30
7 files changed, 175 insertions, 0 deletions
diff --git a/comparison/OCaml/Makefile b/comparison/OCaml/Makefile
new file mode 100644
index 0000000..94a4ffb
--- /dev/null
+++ b/comparison/OCaml/Makefile
@@ -0,0 +1,36 @@
+CC = ocamlopt
+CFLAGS =
+BENCHMARK = bash ../tools/sptest_commands.sh
+#TARGET = ack3_11 fib38 bsort40000 isort40000 qsort800000 msort800000
+TARGET = ack3_11 fib38 bsort40000 isort40000
+LOG = log.txt
+
+
+.PHONY: all
+
+all: $(LOG)
+
+$(LOG): $(TARGET)
+ $(BENCHMARK) $(TARGET)
+
+ack3_11: ack3_11.ml
+ $(CC) $(CFLAGS) $< -o $@
+
+fib38: fib38.ml
+ $(CC) $(CFLAGS) $< -o $@
+
+bsort40000: bsort40000.ml
+ $(CC) $(CFLAGS) $< -o $@
+
+isort40000: isort40000.ml
+ $(CC) $(CFLAGS) $< -o $@
+
+qsort800000: qsort-800000.ml
+ $(CC) $(CFLAGS) $< -o $@
+
+msort800000: msort-800000.ml
+ $(CC) $(CFLAGS) $< -o $@
+
+
+clean:
+ rm -f $(LOG) $(TARGET) *.o *.cmi *.cmx *~
diff --git a/comparison/OCaml/ack3_11.ml b/comparison/OCaml/ack3_11.ml
new file mode 100644
index 0000000..6d4cdce
--- /dev/null
+++ b/comparison/OCaml/ack3_11.ml
@@ -0,0 +1,6 @@
+let rec ack m n =
+ if m==0 then n+1
+ else if n==0 then ack (m-1) 1
+ else ack (m-1) (ack m (n-1))
+
+let () = print_int (ack 3 11);
diff --git a/comparison/OCaml/bsort40000.ml b/comparison/OCaml/bsort40000.ml
new file mode 100644
index 0000000..45744bd
--- /dev/null
+++ b/comparison/OCaml/bsort40000.ml
@@ -0,0 +1,23 @@
+let rec bs (alist : int list) n =
+ match alist with
+ (x::y::ys) ->
+ if x<y then x::(bs (y::ys) (n-1))
+ else y::(bs (x::ys) (n-1))
+ | xs -> xs;;
+
+
+let rec go xs n =
+ match n with
+ 0 -> xs
+ | n -> go (bs xs n) (n-1);;
+
+let bsort xs = go xs (List.length(xs) - 1);;
+
+
+let rec mkRandList n =
+ match n with
+ 0 -> []
+ | n -> (Random.int 10000)::(mkRandList (n-1));;
+
+let main = bsort (mkRandList 40000);;
+
diff --git a/comparison/OCaml/fib38.ml b/comparison/OCaml/fib38.ml
new file mode 100644
index 0000000..2f7906d
--- /dev/null
+++ b/comparison/OCaml/fib38.ml
@@ -0,0 +1,7 @@
+let rec fib n =
+ match n with
+ 0 -> 0
+ | 1 -> 1
+ | n -> fib(n-1) + fib (n-2)
+
+let () = print_int (fib 38);
diff --git a/comparison/OCaml/isort40000.ml b/comparison/OCaml/isort40000.ml
new file mode 100644
index 0000000..686a7f6
--- /dev/null
+++ b/comparison/OCaml/isort40000.ml
@@ -0,0 +1,30 @@
+(*
+let rec insert x ylist =
+ match ylist with
+ [] -> [x]
+ | y::ys when x<=y -> x::y::ys
+ | y::ys -> y::(insert x ys);;
+*)
+
+let rec insert x ylist =
+ match ylist with
+ [] -> [x]
+ | y::ys -> if x<=y
+ then x::y::ys
+ else y::(insert x ys);;
+
+
+let rec isort (alist : int list) =
+ match alist with
+ [] -> []
+ | x::xs -> insert x (isort xs);;
+
+
+
+let rec mkRandList n =
+ match n with
+ 0 -> []
+ | n -> (Random.int 10000)::(mkRandList (n-1));;
+
+let main = isort (mkRandList 40000);;
+
diff --git a/comparison/OCaml/msort800000.ml b/comparison/OCaml/msort800000.ml
new file mode 100644
index 0000000..1835b98
--- /dev/null
+++ b/comparison/OCaml/msort800000.ml
@@ -0,0 +1,43 @@
+let rec split (alist : int list) =
+ match alist with
+ [] -> ([], [])
+ | [a] -> ([a], [])
+ | a::b::cs ->
+ let (m,n) = split cs in
+ (a::m, b::m) ;;
+
+let rec merge (alist : int list) (blist : int list) =
+ match alist with
+ [] -> blist
+ | x::xs ->
+ match blist with
+ [] -> x::xs
+ | y::ys ->
+ if x<y then x::(merge xs (y::ys))
+ else y::(merge (x::xs) ys) ;;
+
+
+let rec msort (alist : int list) =
+ match alist with
+ [] -> []
+ | a::[] -> [a]
+ | a::b::[] -> if a<b then [a;b] else [b;a]
+ | xs ->
+ let (m,n) = split xs
+ in merge (msort m) (msort n) ;;
+
+
+
+let rec mkRandList n =
+ match n with
+ 0 -> []
+ | n -> (Random.int 10000)::(mkRandList (n-1)) ;;
+
+let main = msort (mkRandList 800000) ;;
+
+
+(*
+The following error arises:
+
+Stack overflow during evaluation (looping recursion?).
+*)
diff --git a/comparison/OCaml/qsort800000.ml b/comparison/OCaml/qsort800000.ml
new file mode 100644
index 0000000..627abd2
--- /dev/null
+++ b/comparison/OCaml/qsort800000.ml
@@ -0,0 +1,30 @@
+let rec split pivot (alist : int list) =
+ match alist with
+ [] -> ([], [])
+ | (x::xs) ->
+ let (below, above) = split pivot xs in
+ if x<pivot then (x::below, above)
+ else (below, x::above) ;;
+
+let rec qsort (alist : int list) =
+ match alist with
+ [] -> []
+ | pivot::rest ->
+ let (below, above) = split pivot rest
+ in (qsort below) @ [pivot] @ (qsort above) ;;
+
+
+
+
+let rec mkRandList n =
+ match n with
+ 0 -> []
+ | n -> (Random.int 10000)::(mkRandList (n-1)) ;;
+
+let main = qsort (mkRandList 800000) ;;
+
+(*
+The following error arises:
+
+Stack overflow during evaluation (looping recursion?).
+*)