# [Mono-list] 11 Queens problem slow on mono

dtelford719 Doug at DougTelford.com
Tue Jan 10 20:33:42 EST 2012

```Per your request, this is the code for the 11 queens problem:

open System

(* member of a list.  If have F# powerpack can use List.contains *)
let rec mem x l1 =
if  l1 = []  then false
else if x = (List.head l1) then true
else (mem x (List.tail l1));;

(* are 2 queens safe from each other *)
let rec safe (x1, y1) (x2, y2) =
x1 <> x2 && y1 <> y2 && x2 - x1 <> y2 - y1 && x1 - y2 <> x2 - y1 ;;

(* generate possible solutions *)
let ps n =
[ for i in 1..n do
for j in 1..n do
yield (i,j) ] ;;

(* actually don't use print, commented out *)
let print n qs =
for x in 1 .. n do
for y in 1 .. n do
printf "%s" (if mem (x, y ) qs then "Q" else "." )
printf "\n" ;;

(* primary routine *)
let rec search f n qs ps accu =
match ps with
| []  when List.length qs = n -> f qs accu
| [] -> accu
| q::ps ->
search f n qs ps accu
|> search f n (q::qs) (List.filter (safe q) ps)  ;;

(* solve for n queens on an n x n board *)
let solve n =
let f qs i =
(* print n qs  *) (* don't print solutions *)
i + 1
search f n [] (ps n) 0;;

(* time execution *)
let time f x =
let timer = new System.Diagnostics.Stopwatch()
timer.Start()
try f x finally
printf "Took %d milliseconds \n" timer.ElapsedMilliseconds

(* initial execution *)
let result = time  solve 11;;
printf "number of solutions = %d \n" result;;

--
View this message in context: http://mono.1490590.n4.nabble.com/11-Queens-problem-slow-on-mono-tp4274943p4284143.html
Sent from the Mono - General mailing list archive at Nabble.com.
```