2015-01-04 23:52:50 +01:00
|
|
|
import Data.Function
|
|
|
|
import Data.List
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
-- 1 - sum of numbers ...
|
|
|
|
problem_1 :: Int
|
|
|
|
problem_1 = sum . nub $ [3,6..999] ++ [5,10..999]
|
|
|
|
problem_1' = sum $ union [3,6..999] [5,10..999]
|
2015-11-16 20:56:24 +01:00
|
|
|
main = problem_1'
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
-- bcs.h
|
|
|
|
import Prelude hiding ((>>=), return)
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
type Choice a = [a]
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
choose :: [a] -> Choice a
|
|
|
|
choose xs = xs
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
pair456 :: Int -> Choice (Int, Int)
|
|
|
|
pair456 x = choose [(x, 4), (x, 5), (x, 6)]
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
join :: Choice (Choice a) -> Choice a
|
|
|
|
join = concat
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
-- join $ map pair456 [1,2,3]
|
|
|
|
-- [1, 2, 3] >>= pair456
|
|
|
|
(>>=) :: Choice a -> (a -> Choice b) -> Choice b
|
|
|
|
choices >>= f = join (map f choices)
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
return :: a -> Choice a
|
|
|
|
return x = [x]
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
makePairs :: Choice (Int, Int)
|
|
|
|
makePairs =
|
|
|
|
choose [1, 2, 3] >>= (\x ->
|
|
|
|
choose [4, 5, 6] >>= (\y ->
|
|
|
|
return (x, y)))
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
mzero :: Choice a
|
|
|
|
mzero = choose []
|
2015-01-04 23:52:50 +01:00
|
|
|
|
2015-11-16 20:56:24 +01:00
|
|
|
guard :: Bool -> Choice ()
|
|
|
|
guard True = return ()
|
|
|
|
guard False = mzero
|
|
|
|
|
|
|
|
makePairs' :: Choice (Int,Int)
|
|
|
|
makePairs' = do
|
|
|
|
x <- choose [1,2,3]
|
|
|
|
y <- choose [4,5,6]
|
|
|
|
guard (x*y == 8)
|
|
|
|
return (x,y)
|