1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
(*
//
// HX-2014-03-28:
// A program to solve the 8-queens problem
// based on lazy evaluation
//
*)
(* ****** ****** *)
//
#include
"share/atspre_define.hats"
#include
"share/atspre_staload.hats"
//
#include
"share/HATS/atspre_staload_libats_ML.hats"
//
(* ****** ****** *)
//
staload UN = $UNSAFE
//
(* ****** ****** *)
fun path_test
(
xs: list0 (int), x0: int
) : bool = let
//
fun aux
(
xs: list0 (int), df: int
) : bool =
(
case+ xs of
| nil0 () => true
| cons0 (x, xs) =>
if x0 != x && abs (x0 - x) != df then aux (xs, df+1) else false
// end of [cons0]
)
//
in
aux (xs, 1(*df*))
end // end of [path_test]
(* ****** ****** *)
#define N 8
(* ****** ****** *)
fun path_next
(
xs: list0(int)
) : stream(list0(int)) = $delay
(
let
val n = length (xs)
in
//
if n < N
then path_next2 (xs, 0)
else let
val-cons0 (x, xs) = xs
in
path_next2 (xs, x + 1)
end // end of [else]
//
end
) // end of [path_next]
and path_next2
(
xs: list0(int), x0: int
) : stream_con(list0(int)) =
(
if x0 < N
then let
val pass = path_test (xs, x0)
in
if pass
then let
val xs = cons0 (x0, xs)
in
stream_cons (xs, path_next (xs))
end // end of [then]
else path_next2 (xs, x0 + 1)
// end of [if]
end // end of [then]
else (
case+ xs of
| cons0 (x, xs) => path_next2 (xs, x+1) | nil0 () => stream_nil(*void*)
) (* end of [else] *)
) (* end of [path_next2] *)
(* ****** ****** *)
val theSolutions =
stream_filter_cloref (path_next (nil0(*void*)), lam (xs) => length (xs) >= N)
// end of [theSolutions]
(* ****** ****** *)
fun fprint_row
(
out: FILEref, i: int
) : void = let
//
fun aux (i: int): void =
if i > 0 then (fprint (out, ". "); aux (i-1)) else ()
//
val () = aux (i)
val () = fprint (out, "Q ")
val () = aux (N-1-i)
//
in
// nothing
end // end of [fprint_row]
(* ****** ****** *)
fun fprint_board
(
out: FILEref, xs: list0(int)
) : void = let
//
implement
fprint_val<int> (out, x) = fprint_row (out, x)
//
val () =
fprint_list0_sep (out, xs, "\n")
val () = fprint_newline (out)
//
in
// nothing
end // end of [fprint_board]
(* ****** ****** *)
val () = let
//
fun printall
(
xss: stream(list0(int)), i: int
) : void =
(
case+ !xss of
| stream_cons
(xs, xss) =>
{
val () =
println! ("Solution #", i, ":")
val () = fprint_board (stdout_ref, list0_reverse(xs))
val () = print_newline ()
val () = printall (xss, i+1)
}
| stream_nil () => ()
)
//
in
printall (theSolutions, 1)
end // end of [val]
(* ****** ****** *)
implement main0 () = ()
(* ****** ****** *)
(* end of [queens_lazy.dats] *)
|