Skip to content

Commit

Permalink
roman-numerals: test for Nothing (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
kephas authored Feb 20, 2024
1 parent 3932dd3 commit cee99f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Roman (numerals) where

numerals :: Int -> Maybe String
numerals = Just . go numeralMap
numerals number | number < 1 || number > 3999 = Nothing
numerals number = Just $ go numeralMap number
where
go pairs@((value, digits):pairs') n
| n >= value = digits ++ go pairs (n - value)
Expand Down
12 changes: 12 additions & 0 deletions exercises/practice/roman-numerals/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ cases = [ Case { description = "1 is a single I"
, number = 3000
, expected = Just "MMM"
}
, Case { description = "4000 cannot be represented"
, number = 4000
, expected = Nothing
}
, Case { description = "0 cannot be represented"
, number = 0
, expected = Nothing
}
, Case { description = "-1 cannot be represented"
, number = -1
, expected = Nothing
}
]

0 comments on commit cee99f1

Please sign in to comment.