aboutsummaryrefslogtreecommitdiffstats
path: root/utils.hs
diff options
context:
space:
mode:
Diffstat (limited to 'utils.hs')
-rw-r--r--utils.hs10
1 files changed, 9 insertions, 1 deletions
diff --git a/utils.hs b/utils.hs
index e7e0d4b..c67fd07 100644
--- a/utils.hs
+++ b/utils.hs
@@ -1,6 +1,7 @@
module Utils where
import Control.Applicative (ZipList (ZipList, getZipList))
+import Data.Char (digitToInt)
(.>) = flip (.)
@@ -46,4 +47,11 @@ takeUntil _ [] = []
takeUntil p (x : xs) = x : if p x then takeUntil p xs else []
fromSymEither (Left x) = x
-fromSymEither (Right x) = x \ No newline at end of file
+fromSymEither (Right x) = x
+
+-- | Convert binary (list of zeros and ones) to decimal
+binaryToInt :: String -> Int
+binaryToInt = reverse .> binaryToIntRev
+
+binaryToIntRev [] = 0
+binaryToIntRev (x : xs) = digitToInt x + 2 * binaryToIntRev xs