-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathq11.hs
81 lines (66 loc) · 2.33 KB
/
q11.hs
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
infixl 9 !!;
infixr 9 .;
infixl 7 * , / , %;
infixl 6 + , -;
infixr 5 ++;
infixl 4 <*>;
infix 4 == , <=;
infixl 3 &&;
infixl 2 ||;
infixl 1 >> , >>=;
infixr 0 $;
foreign import ccall "putchar" putChar :: Int -> IO Int;
class Eq a where { (==) :: a -> a -> Bool };
instance Eq Int where { (==) = intEq };
class Ord a where { (<=) :: a -> a -> Bool };
instance Ord Int where { (<=) = intLE };
class Functor f where { fmap :: (a -> b) -> f a -> f b };
class Applicative f where
{ pure :: a -> f a
; (<*>) :: f (a -> b) -> f a -> f b
};
class Monad m where
{ return :: a -> m a
; (>>=) :: m a -> (a -> m b) -> m b
};
(>>) f g = f >>= \_ -> g;
($) f x = f x;
(.) f g x = f (g x);
id x = x;
flip f x y = f y x;
-- TOOD: Support signed ints.
-- abs x = if 0 <= x then x else 0 - x;
abs x = if x <= 2147483647 then x else 0 - x;
not a = if a then False else True;
(||) f g = if f then True else g;
(&&) f g = if f then g else False;
flst xs n c = case xs of { [] -> n; h:t -> c h t };
foldr c n l = flst l n (\h t -> c h(foldr c n t));
(++) = flip (foldr (:));
concat = foldr (++) [];
map = flip (foldr . ((:) .)) [];
concatMap = (concat .) . map;
and = foldr (&&) True;
undefined = undefined;
xs !! n = flst xs undefined \x xt -> if n == 0 then x else xt !! (n - 1);
checks q b i = q==b!!i || abs(q-b!!i)==i+1;
index x = let
{ f n [] = []
; f n (a:x) = n:f(n+1)x
} in f 0 x;
safe q b = and $ map (not . checks q b) $ index b;
range m n | m <= n = m:range (m+1) n | True = [];
queens 0 = [[]];
queens n = concatMap (\b -> concatMap (\q -> concatMap (\_ -> [q:b])
$ if safe q b then [()] else []) $ range 1 11) $ queens $ n - 1;
instance Applicative IO where { pure = ioPure ; (<*>) f x = ioBind f \g -> ioBind x \y -> ioPure (g y) };
instance Monad IO where { return = ioPure ; (>>=) = ioBind };
instance Functor IO where { fmap f x = ioPure f <*> x };
mapM_ f = foldr ((>>) . f) (pure ());
putStr = mapM_ $ putChar . ord;
class Shows a where { shows :: a -> String -> String };
showInt' n = if 0 == n then id else (showInt' $ n/10) . ((:) (chr $ 48+n%10));
instance Shows Int where { shows n = if 0 == n then ('0':) else showInt' n };
intersperse x as = flst as [] \a at -> a : foldr (\h t -> [x, h] ++ t) [] at;
instance Shows a => Shows [a] where { shows xs = ('[':) . foldr (.) id (intersperse (',':) $ map shows xs) . (']':) };
main = putStr $ shows (queens 11) "";