module lqueen // The Queens Problem, slow version. (&&) infixr 3 :: !Bool Bool -> Bool (&&) a b = code { push_b 0 jmp_false l1 pop_b 1 jsr_eval 0 pushB_a 0 pop_a 1 .d 0 1 b rtn :l1 pop_a 1 .d 0 1 b rtn } (||) infixr 2 :: !Bool Bool -> Bool (||) a b = code { push_b 0 jmp_true l2 pop_b 1 jsr_eval 0 pushB_a 0 pop_a 1 .d 0 1 b rtn :l2 pop_a 1 .d 0 1 b rtn } not :: !Bool -> Bool not True = False not False = True (<) infix 4 :: !Int !Int -> Bool (<) a b = code inline { ltI } (>) a b :== b < a (+) infixl 6 :: !Int !Int -> Int (+) a b = code inline { addI } inc :: !Int -> Int inc a = a + 1 (-) infixl 6 :: !Int !Int -> Int (-) a b = code inline { subI } (==) infix 4 :: !Int !Int -> Bool (==) a b = code inline { eqI } (<>) infix 4 :: !Int !Int -> Bool (<>) a b = not (a == b) length :: [a] -> Int length xs = len 0 xs where len :: Int [a] -> Int len i [] = i len i [_:xs] = len (i+1) xs hd :: [a] -> a hd [x:xs] = x //import StdEnv BoardSize :== 8 // The size of the chessboard. // Finding all solutions for the queens problem. Queens::Int [Int] [[Int]] -> [[Int]] Queens row board boards | row>BoardSize = [board:boards] | otherwise = TryCols BoardSize row board boards // The second alternative of TryCols is added to make sure Save is never // called with an empty list. TryCols::Int Int [Int] [[Int]] -> [[Int]] TryCols 0 row board boards = boards TryCols col row [] boards = TryCols (col-1) row [] queens where queens = Queens (row+1) [col] boards TryCols col row board boards | Save col 1 board = TryCols (col-1) row board queens | otherwise = TryCols (col-1) row board boards where queens = Queens (row+1) [col : board] boards Save::Int Int [Int] -> Bool Save c1 rdiff [c2] = cdiff<>0 && cdiff<>rdiff && cdiff<> 0 - rdiff where cdiff = c1 - c2 Save c1 rdiff [c2:cols] | cdiff==rdiff || cdiff==0 || cdiff==0-rdiff = False | otherwise = Save c1 (inc rdiff) cols where cdiff = c1 - c2 /* The Start Rule: Calculate the list of solutions, show the first solution and the length of that list. */ Start::(Int,[Int]) Start = (length solutions, hd solutions) where solutions = Queens 1 [] []