aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Utils.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/Utils.hs b/lib/Utils.hs
new file mode 100644
index 0000000..1eb42fc
--- /dev/null
+++ b/lib/Utils.hs
@@ -0,0 +1,32 @@
1module Utils ( binaryToInt
2 , countElem
3 , xor
4 , right
5 , bet
6 , (&+)
7 ) where
8
9import Data.Char (digitToInt)
10import Control.Monad
11import Data.Either
12
13
14binaryToInt :: String -> Int
15binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0
16
17countElem :: (Eq a) => a -> [a] -> Int
18countElem c ls = sum $ map (fromEnum . (== c)) ls
19
20xor :: Bool -> Bool -> Bool
21xor a b = (not a && b) || (a && not b)
22
23right :: Either a b -> b
24right (Right b) = b
25right _ = undefined
26
27bet :: Int -> (Int, Int) -> Bool
28bet k (l, u) = k >= l && k <= u
29
30-- combine filter predicates
31(&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
32(&+) = liftM2 (&&)