fun safe x y ((x1,y1)::xs) = x <> x1 (* not same column *) andalso y <> y1 (* not same row *) andalso y - x <> y1 - x1 (* not on /-diagonal *) andalso y + x <> y1 + x1 (* not on \-diagonal *) andalso safe x y xs | safe _ _ _ = true exception Conflict fun queens4 n = let fun queens x y posl = if x > n then posl (* 1 *) else if y > n then raise Conflict (* 2.b *) else (if safe x y posl (* 2.a *) then queens (x+1) 1 ((x,y) :: posl) else raise Conflict) handle Conflict => queens x (y+1) posl in queens 1 1 [] end val doit = let val sol = queens4 (valOf(Int.fromString(hd(CommandLine.arguments())))) in app (fn(x,y) => app print ["(",Int.toString x,", ", Int.toString y,")\n"]) sol end