all stats

lexi's stats

guessed the most

namecorrect guessesgames togetherratio

were guessed the most by

namecorrect guessesgames togetherratio

entries

round #102

submitted at
0 likes

guesses
comments 0

post a comment


Set.hs Unicode text, UTF-8 text
  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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{-# LANGUAGE NoImplicitPrelude #-}

module Set (
  Set
, empty, singleton
, findMin, deleteMin, findMax, deleteMax
, succ, pred, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve
, false, true, iif, not, (||), (&&)
, (==), (/=)
, lt, eq, gt, compare, (>=)
, (), union, (), intersection, (), symmetricDifference, (\\), difference
, (<<), insert, (>>), delete
, (), member, (), notMember, (), isSubsetOf, disjoint
, filter, map, flatmap, any, all, unions
, size, take, drop, range, powerSet
, (+), (-), (*), (/), (%), mod
, pair, fst, snd, (×), cartesianProduct, partition
) where

import Prelude (Show(show), (++))

infixl 9 *
infixl 9 /
infixl 9 %
infixl 9 `mod`
infixl 9 ×
infixl `cartesianProduct`
infixl 8 +
infixl 8 -
infixl 8 \\
infixl 8 `difference`
infixl 8 <<
infixl 8 >>
infixl 7 
infixl 7 `intersection`
infixl 6 
infixl 6 `symmetricDifference`
infixl 5 
infixl 5 `union`
infix  4 ==
infix  4 /=
infix  4 >=
infix  4 
infix  4 `member`
infix  4 
infix  4 `notMember`
infix  4 
infix  4 `isSubsetOf`
infixl 3 &&
infixl 2 ||

data Set = Nil | Cons Set Set

empty = Nil
singleton a = Cons a Nil

-- Min/Max
findMin (Cons a _) = a

deleteMin Nil        = empty
deleteMin (Cons _ a) = a

findMax (Cons a Nil) = a
findMax (Cons _ a  ) = findMax a

deleteMax Nil          = empty
deleteMax (Cons a Nil) = empty
deleteMax (Cons a b  ) = Cons a (deleteMax b)

-- Zermelo ordinals
succ = singleton
pred (Cons a Nil) = a

zero   = empty
one    = succ zero
two    = succ one
three  = succ two
four   = succ three
five   = succ four
six    = succ five
seven  = succ six
eight  = succ seven
nine   = succ eight
ten    = succ nine
eleven = succ ten
twelve = succ eleven

-- Logic
false = zero
true  = one

iif Nil _ a = a
iif _   a _ = a

not a  = iif a false true
a || b = iif a a b
a && b = iif a b a

-- Equality
Nil == Nil = true
Nil == _   = false
_   == Nil = false
(Cons a as) == (Cons b bs) = a == b && as == bs

a /= b = not (a == b)

-- Comparison
lt = zero
eq = one
gt = two
compare Nil Nil = eq
compare Nil _   = lt
compare _   Nil = gt
compare (Cons a as) (Cons b bs) = iif (a == b) (compare as bs) (compare a b)
(>=) = compare

-- Set operations
Nil  a   = a
a    Nil = a
(Cons a as)  (Cons b bs) = case compare a b of
  Nil          -> Cons a (as  Cons b bs)
  Cons Nil Nil -> Cons a (as  bs)
  _            -> Cons b (Cons a as  bs)
union = ()

Nil  _   = empty
_    Nil = empty
(Cons a as)  (Cons b bs) = case compare a b of
  Nil          -> as  Cons b bs
  Cons Nil Nil -> Cons a (as  bs)
  _            -> Cons a as  bs
intersection = ()

Nil  a   = a
a    Nil = a
(Cons a as)  (Cons b bs) = case compare a b of
  Nil          -> Cons a (as  Cons b bs)
  Cons Nil Nil -> as  bs
  _            -> Cons b (Cons a as  bs)
symmetricDifference = ()

Nil \\ _   = empty
a   \\ Nil = a
(Cons a as) \\ (Cons b bs) = case compare a b of
  Nil          -> Cons a (as \\ Cons b bs)
  Cons Nil Nil -> as \\ bs
  _            -> Cons a as \\ bs
difference = (\\)

a << b = a  singleton b -- ruby <3
insert a b = b << a
a >> b = a \\ singleton b
delete a b = b >> a

_  Nil         = false
a  (Cons b bs) = a == b || a  bs
member = ()
a  b = not (a  b)
notMember = ()

Nil  _   = true
_    Nil = false
(Cons a as)  (Cons b bs) = case compare a b of
  Nil          -> false
  Cons Nil Nil -> as  bs
  _            -> Cons a as  bs
isSubsetOf = ()

disjoint a b = not (a  b)

filter _ Nil         = empty
filter f (Cons a as) = iif (f a) (Cons a t) t
  where t = filter f as

map _ Nil         = empty
map f (Cons a as) = map f as << f a

flatmap _ Nil         = empty
flatmap f (Cons a as) = flatmap f as  f a

any _ Nil         = false
any p (Cons a as) = p a || any p as

all _ Nil         = true
all p (Cons a as) = p a && all p as

unions Nil         = empty
unions (Cons a as) = a  unions as

size Nil        = zero
size (Cons _ a) = succ (size a)

take _   Nil         = empty
take Nil _           = empty
take n   (Cons a as) = Cons a (take (pred n) as)

drop Nil a           = a
drop _   Nil         = empty
drop n   (Cons a as) = drop (pred n) as

nats = go zero
  where go n = Cons n (go (succ n))

range n = take n nats

powerSet Nil         = singleton empty
powerSet (Cons a as) = p  map (<< a) p
  where p = powerSet as

-- Arithmetic
a + b = iif b (succ a + pred b) a
a - b = iif b (pred a - pred b) a
a * b = iif b (a + a * pred b) zero
a / b = iif (a >= b) (succ ((a - b) / b)) zero
a % b = iif (a >= b) (mod (a - b) b) a
mod = (%)

-- Tuples
-- Wiener construction:
-- (a,b) = {     {    { }  ,     {    a   }    }    ,     {     {    b   }    }    }
pair a b = Cons (Cons Nil (Cons (Cons a Nil) Nil)) (Cons (Cons (Cons b Nil) Nil) Nil)

fst (Cons (Cons Nil (Cons (Cons a Nil) Nil)) _) = a
snd (Cons _ (Cons (Cons (Cons a Nil) Nil) Nil)) = a

as × bs = flatmap (\a -> map (pair a) bs) as
cartesianProduct = (×)

partition _ Nil         = pair Nil Nil
partition p (Cons a as) = iif (p a) (pair (Cons a f) s) (pair f (Cons a s))
  where t = partition p as
        f = fst t
        s = snd t

-- Debugging
valid Nil                  = true
valid (Cons a Nil)         = valid a
valid (Cons a (Cons b bs)) = valid a && not (a >= b) && valid (Cons b bs)

instance Show Set where
  show Nil = "{}"
  show a   = "{" ++ show' a
    where show' (Cons a as) = show a ++ iif as ("," ++ show' as) "}"
main.hs ASCII text
 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
{-# LANGUAGE NoImplicitPrelude #-}

import Prelude (print)
import Set

nqueens n = (nqueens' empty zero (range n))
  where
  compatible xa ya b = xa + ya /= xb + yb && xa + yb /= xb + ya
    where xb = fst b
          yb = snd b
  nqueens' sol y xs = iif (y == n) sol (
    any (\x -> nqueens' (sol << pair x y) (succ y) (xs >> x))
    (filter (\x -> all (compatible x y) sol) xs) )

nqueensAll n = (nqueensAll' empty zero (range n))
  where
  compatible xa ya b = xa + ya /= xb + yb && xa + yb /= xb + ya
    where xb = fst b
          yb = snd b
  nqueensAll' sol y xs = iif (y == n) (singleton sol) (
    flatmap (\x -> nqueensAll' (sol << pair x y) (succ y) (xs >> x))
    (filter (\x -> all (compatible x y) sol) xs) )

-- verify there are 14,200 solutions for n = 12
main = print (size (nqueensAll twelve) == (ten * ten + six * seven) * ten * ten)