aboutsummaryrefslogtreecommitdiff
path: root/comparison/OCaml
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--comparison/OCaml/Makefile5
-rw-r--r--comparison/OCaml/nqueen12.ml23
2 files changed, 27 insertions, 1 deletions
diff --git a/comparison/OCaml/Makefile b/comparison/OCaml/Makefile
index 9db7726..18c1eda 100644
--- a/comparison/OCaml/Makefile
+++ b/comparison/OCaml/Makefile
@@ -1,7 +1,7 @@
CC = ocamlopt
CFLAGS =
BENCHMARK = bash ../tools/sptest_commands.sh
-TARGET = ack3_11 fib38 bsort20000 isort20000 qsort260000 msort260000
+TARGET = nqueen12 ack3_11 fib38 bsort20000 isort20000 qsort260000 msort260000
LOG = log.txt
@@ -12,6 +12,9 @@ all: $(LOG)
$(LOG): $(TARGET)
$(BENCHMARK) $(TARGET)
+nqueen12: nqueen12.ml
+ $(CC) $(CFLAGS) $< -o $@
+
ack3_11: ack3_11.ml
$(CC) $(CFLAGS) $< -o $@
diff --git a/comparison/OCaml/nqueen12.ml b/comparison/OCaml/nqueen12.ml
new file mode 100644
index 0000000..fc56542
--- /dev/null
+++ b/comparison/OCaml/nqueen12.ml
@@ -0,0 +1,23 @@
+let rec member x alist =
+ match alist with
+ [] -> false
+ | y::ys -> if x=y then true else (member x ys);;
+
+let rec threat k m alist =
+ match alist with
+ [] -> false
+ | x::xs -> if (k = x-m) || (k = m-x) then true
+ else (threat (k+1) m xs);;
+
+let rec queen1 m b n =
+ match m with
+ 0 -> []
+ | m ->
+ if (member m b) || (threat 1 m b) then (queen1 (m-1) b n)
+ else if (List.length b) = (n-1) then [m::b]@(queen1 (m-1) b n)
+ else (queen1 n (m::b) n) @ (queen1 (m-1) b n);;
+
+let queen n = queen1 n [] n;;
+
+let n = 12;;
+let () = print_int (List.length (queen n));