moocfi / haskell-mooc

Haskell MOOC University of Helsinki
Other
313 stars 446 forks source link

Set15 Ex7: Ambiguity with non decimal representation of integers #29

Closed Preoo closed 3 years ago

Preoo commented 3 years ago

This is minor "issue" but following implementation for exercise 7

boolOrInt :: String -> Validation (Either Bool Int)
boolOrInt s = isBool s <|> isInt s
  where isInt  = maybe (invalid "Not an Int") (pure . Right) . readMaybe
        isBool = maybe (invalid "Not a Bool") (pure . Left)  . readMaybe

passes tests until string with valid integer in non decimal numeral system such as 0x1 or 0o2 is generated.

Example of failure:

boolOrInt "0x1" 
got:      Ok (Right 1) 
expected: Errors ["Not a Bool","Not an Int"]

Suggestions to help student catch/avoid this edge case

a. add a test case to enforce valid integers to be decimals

b. modify tests to allow non decimal integers

c. instruct students to regard valid integers to be only in decimal form and raise error otherwise in exercise description

opqdonut commented 3 years ago

good catch, thanks!