From 02a84743ddf5c8c27a36c4d5a20eaf3cd2a9aff4 Mon Sep 17 00:00:00 2001 From: Shinya Sato Date: Sun, 28 Aug 2022 20:40:57 +0900 Subject: Update for n-queens --- comparison/OCaml/Makefile | 5 ++++- comparison/OCaml/nqueen12.ml | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 comparison/OCaml/nqueen12.ml (limited to 'comparison/OCaml') 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)); -- cgit v1.2.3