Idris2/tests/idris2/reg050/loopy.idr
Edwin Brady 2a5739c27a
Check primitives (fromInteger etc) reduce on LHS (#1903)
If they don't, we can't turn them into patterns to match on, and we end
up looping. Possibly we could throw a different and maybe more
informative error instead of just making an unmatchable pattern.
Fixes #1895
2021-09-05 12:37:59 +01:00

28 lines
682 B
Idris

toBit : Num i => Char -> Maybe i
toBit '0' = Just 0
toBit '1' = Just 1
toBit _ = Nothing
-- Return the number represented in binary as an initial segment of
-- the string (remembering that if a number begins 0, it's zero).
eatBinary : (Eq i, Num i) => String -> i
eatBinary = firstDigit . unpack where
laterDigit : i -> List Char -> i
laterDigit n [] = n
laterDigit n (d :: ds) = let
f : Maybe i -> i
f Nothing = n
f (Just a) = laterDigit (2*n + a) ds
in f $ toBit d
firstDigit : List Char -> i
firstDigit [] = 0
firstDigit (d :: ds) = let
f : Maybe i -> i
f Nothing = 0
f (Just 0) = 0
f (Just a) = laterDigit a ds
in f $ toBit d