aboutsummaryrefslogtreecommitdiff
path: root/comparison/SML
diff options
context:
space:
mode:
Diffstat (limited to 'comparison/SML')
-rw-r--r--comparison/SML/Makefile15
-rw-r--r--comparison/SML/ack3-11.sml6
-rw-r--r--comparison/SML/bsort-40000.sml30
-rw-r--r--comparison/SML/fib38.sml9
-rw-r--r--comparison/SML/isort-40000.sml21
-rw-r--r--comparison/SML/msort-800000.sml45
-rw-r--r--comparison/SML/qsort-800000.sml27
7 files changed, 153 insertions, 0 deletions
diff --git a/comparison/SML/Makefile b/comparison/SML/Makefile
new file mode 100644
index 0000000..8520e9d
--- /dev/null
+++ b/comparison/SML/Makefile
@@ -0,0 +1,15 @@
+COMMAND = sml
+TARGET = ack3-11.sml fib38.sml bsort-40000.sml isort-40000.sml qsort-800000.sml msort-800000.sml
+BENCHMARK = bash ../tools/sptest_scripts.sh
+LOG = log.txt
+
+
+.PHONY: all
+
+all: $(LOG)
+
+$(LOG): $(TARGET)
+ $(BENCHMARK) $(COMMAND) '$(TARGET)'
+
+clean:
+ rm -f $(LOG) *~
diff --git a/comparison/SML/ack3-11.sml b/comparison/SML/ack3-11.sml
new file mode 100644
index 0000000..f1ea7f2
--- /dev/null
+++ b/comparison/SML/ack3-11.sml
@@ -0,0 +1,6 @@
+fun ack 0 n = n+1
+| ack m 0 = ack (m-1) 1
+| ack m n = ack (m-1) (ack m (n-1));
+
+
+ack 3 11;
diff --git a/comparison/SML/bsort-40000.sml b/comparison/SML/bsort-40000.sml
new file mode 100644
index 0000000..48c3eb4
--- /dev/null
+++ b/comparison/SML/bsort-40000.sml
@@ -0,0 +1,30 @@
+(*
+https://gist.github.com/masquerade0324/68f4f3fd3ab897fbe1ad
+*)
+
+fun bs xs 0 = xs
+ | bs (x::y::ys) n =
+ if (x<y) then x::(bs (y::ys) (n-1))
+ else y::(bs (x::ys) (n-1))
+ | bs xs _ = xs;
+
+fun go xs 0 = xs
+ | go xs n = go (bs xs n) (n-1);
+
+
+fun bsort xs = go xs (length(xs) - 1);
+
+
+(* mkRandList *)
+local
+ val nextInt = Random.randRange(1,10000);
+ val r = Random.rand(1,1);
+in
+ fun mkRandList 0 = []
+ | mkRandList n = (nextInt r)::(mkRandList (n-1))
+end;
+
+
+bsort (mkRandList 40000);
+
+
diff --git a/comparison/SML/fib38.sml b/comparison/SML/fib38.sml
new file mode 100644
index 0000000..c49ed14
--- /dev/null
+++ b/comparison/SML/fib38.sml
@@ -0,0 +1,9 @@
+fun fib 0 = 0
+| fib 1 = 1
+| fib n = (fib (n-1)) + (fib (n-2));
+
+
+
+(* ex *)
+(* prnat(Ack (mknat 2) (mknat 3)); *)
+fib 38;
diff --git a/comparison/SML/isort-40000.sml b/comparison/SML/isort-40000.sml
new file mode 100644
index 0000000..973d6e6
--- /dev/null
+++ b/comparison/SML/isort-40000.sml
@@ -0,0 +1,21 @@
+(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *)
+fun insertsort [] = []
+ | insertsort (x::xs) =
+ let fun insert (x, []) = [x]
+ | insert (x, y::ys) =
+ if x<=y then x::y::ys
+ else y::insert(x, ys)
+ in insert(x, insertsort xs)
+ end;
+
+
+(* mkRandList *)
+local
+ val nextInt = Random.randRange(1,10000);
+ val r = Random.rand(1,1);
+in
+ fun mkRandList 0 = []
+ | mkRandList n = (nextInt r)::(mkRandList (n-1))
+end;
+
+insertsort (mkRandList 40000); \ No newline at end of file
diff --git a/comparison/SML/msort-800000.sml b/comparison/SML/msort-800000.sml
new file mode 100644
index 0000000..2a33649
--- /dev/null
+++ b/comparison/SML/msort-800000.sml
@@ -0,0 +1,45 @@
+(* https://stackoverflow.com/questions/25839939/understanding-merge-sort-in-ml *)
+
+fun split [] = ([], [])
+ | split [a] = ([a], [])
+ | split (a::b::cs) =
+ let
+ val (m,n) = split cs
+ in
+ (a::m, b::n)
+ end;
+
+fun merge ([], ys) = ys
+ | merge (xs, []) = xs
+ | merge (x::xs, y::ys) =
+ if x<y then
+ x::merge(xs, y::ys)
+ else
+ y::merge(x::xs, ys);
+
+fun msort [] = []
+ | msort [a] = [a]
+ | msort [a,b] = if a<b then [a,b] else [b,a]
+ | msort xs =
+ let
+ val (m,n) = split xs;
+ in
+ merge (msort m, msort n)
+ end;
+
+
+
+
+(* mkRandList *)
+local
+ val nextInt = Random.randRange(1,10000);
+ val r = Random.rand(1,1);
+in
+ fun mkRandList 0 = []
+ | mkRandList n = (nextInt r)::(mkRandList (n-1))
+end;
+
+
+msort (mkRandList 800000);
+
+
diff --git a/comparison/SML/qsort-800000.sml b/comparison/SML/qsort-800000.sml
new file mode 100644
index 0000000..e27af56
--- /dev/null
+++ b/comparison/SML/qsort-800000.sml
@@ -0,0 +1,27 @@
+fun quicksort nil = nil
+ | quicksort (pivot :: rest) =
+ let
+ fun split(nil) = (nil,nil)
+ | split(x :: xs) =
+ let
+ val (below, above) = split(xs)
+ in
+ if x < pivot then (x :: below, above)
+ else (below, x :: above)
+ end;
+ val (below, above) = split(rest)
+ in
+ quicksort below @ [pivot] @ quicksort above
+ end;
+
+
+(* mkRandList *)
+local
+ val nextInt = Random.randRange(1,10000);
+ val r = Random.rand(1,1);
+in
+ fun mkRandList 0 = []
+ | mkRandList n = (nextInt r)::(mkRandList (n-1))
+end;
+
+quicksort (mkRandList 800000);