-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from shwestrick/nqueens
nqueens example
- Loading branch information
Showing
6 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ PROGRAMS= \ | |
ray \ | ||
tokens \ | ||
nn \ | ||
dedup | ||
dedup \ | ||
nqueens | ||
|
||
all: $(PROGRAMS) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
structure FuncSequence: | ||
sig | ||
type 'a t | ||
type 'a seq = 'a t | ||
|
||
val empty: unit -> 'a seq | ||
val length: 'a seq -> int | ||
val take: 'a seq -> int -> 'a seq | ||
val drop: 'a seq -> int -> 'a seq | ||
val nth: 'a seq -> int -> 'a | ||
val iterate: ('b * 'a -> 'b) -> 'b -> 'a seq -> 'b | ||
val tabulate: (int -> 'a) -> int -> 'a seq | ||
val reduce: ('a * 'a -> 'a) -> 'a -> 'a seq -> 'a | ||
end = | ||
struct | ||
(* (i, j, f) defines the sequence [ f(k) : i <= k < j ] *) | ||
type 'a t = int * int * (int -> 'a) | ||
type 'a seq = 'a t | ||
|
||
fun empty () = (0, 0, fn _ => raise Subscript) | ||
fun length (i, j, _) = j - i | ||
fun nth (i, j, f) k = f (i+k) | ||
fun take (i, j, f) k = (i, i+k, f) | ||
fun drop (i, j, f) k = (i+k, j, f) | ||
|
||
fun tabulate f n = (0, n, f) | ||
|
||
fun iterate f b s = | ||
if length s = 0 then b | ||
else iterate f (f (b, nth s 0)) (drop s 1) | ||
|
||
fun reduce f b s = | ||
case length s of | ||
0 => b | ||
| 1 => nth s 0 | ||
| n => let | ||
val half = n div 2 | ||
val (l, r) = | ||
ForkJoin.par (fn _ => reduce f b (take s half), | ||
fn _ => reduce f b (drop s half)) | ||
in | ||
f (l, r) | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lib/sources.mlb | ||
nqueens.sml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
type board = (int * int) list | ||
|
||
fun threatened (i,j) [] = false | ||
| threatened (i,j) ((x,y)::Q) = | ||
i = x orelse j = y orelse i-j = x-y orelse i+j = x+y | ||
orelse threatened (i,j) Q | ||
|
||
structure Seq = FuncSequence | ||
|
||
fun countSol n = | ||
let | ||
fun search i b = | ||
if i >= n then 1 else | ||
let | ||
fun tryCol j = | ||
if threatened (i, j) b then 0 else search (i+1) ((i,j)::b) | ||
in | ||
if i >= 3 then | ||
(* if we're already a few levels deep, then just go sequential *) | ||
Seq.iterate op+ 0 (Seq.tabulate tryCol n) | ||
else | ||
Seq.reduce op+ 0 (Seq.tabulate tryCol n) | ||
end | ||
in | ||
search 0 [] | ||
end | ||
|
||
val n = CommandLineArgs.parseInt "N" 13 | ||
val _ = print ("counting number of " ^ | ||
Int.toString n ^ "x" ^ Int.toString n ^ " solutions\n") | ||
|
||
val t0 = Time.now () | ||
val result = countSol n | ||
val t1 = Time.now () | ||
|
||
val _ = print ("finished in " ^ Time.fmt 4 (Time.- (t1, t0)) ^ "s\n") | ||
|
||
val _ = print ("result " ^ Int.toString result ^ "\n") |