From 555da2ae6f658672cfc0d37e437ec356c0c0fa63 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 5 Dec 2020 13:19:25 +0530 Subject: factor common functions into Utils --- lib/Utils.hs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/Utils.hs (limited to 'lib') 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 @@ +module Utils ( binaryToInt + , countElem + , xor + , right + , bet + , (&+) + ) where + +import Data.Char (digitToInt) +import Control.Monad +import Data.Either + + +binaryToInt :: String -> Int +binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0 + +countElem :: (Eq a) => a -> [a] -> Int +countElem c ls = sum $ map (fromEnum . (== c)) ls + +xor :: Bool -> Bool -> Bool +xor a b = (not a && b) || (a && not b) + +right :: Either a b -> b +right (Right b) = b +right _ = undefined + +bet :: Int -> (Int, Int) -> Bool +bet k (l, u) = k >= l && k <= u + +-- combine filter predicates +(&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) +(&+) = liftM2 (&&) -- cgit v1.2.3