Skip to content

Commit 86312ca

Browse files
authored
Merge pull request #163 from samjonester/phone-number-maybes
Changing return types for phone-number exercise to be Maybes
2 parents cc39bb7 + 62fc8a7 commit 86312ca

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

exercises/phone-number/example.hs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
module Phone (number, areaCode, prettyPrint) where
22
import Data.Char (isDigit)
33

4-
number :: String -> String
4+
number :: String -> Maybe String
55
number input
6-
| len == 10 = digits
7-
| len == 11 && head digits == '1' = tail digits
8-
| otherwise = replicate 10 '0'
6+
| len == 10 = Just digits
7+
| len == 11 && head digits == '1' = Just $ tail digits
8+
| otherwise = Nothing
99
where digits = filter isDigit input
1010
len = length digits
1111

12-
parts :: String -> (String, String, String)
13-
parts input = (ac, exchange, subscriber)
14-
where
15-
(ac, exchangeSubscriber) = splitAt 3 (number input)
16-
(exchange, subscriber) = splitAt 3 exchangeSubscriber
12+
parts :: String -> Maybe (String, String, String)
13+
parts input = case number input of
14+
Nothing -> Nothing
15+
Just digits -> Just (ac, exchange, subscriber)
16+
where
17+
(ac, exchangeSubscriber) = splitAt 3 digits
18+
(exchange, subscriber) = splitAt 3 exchangeSubscriber
1719

18-
areaCode :: String -> String
19-
areaCode input = ac
20-
where (ac, _, _) = parts input
20+
areaCode :: String -> Maybe String
21+
areaCode input = case parts input of
22+
Just (ac, _, _) -> Just ac
23+
Nothing -> Nothing
2124

22-
prettyPrint :: String -> String
23-
prettyPrint input = "(" ++ ac ++ ") " ++ exchange ++ "-" ++ subscriber
24-
where (ac, exchange, subscriber) = parts input
25+
prettyPrint :: String -> Maybe String
26+
prettyPrint input = case parts input of
27+
Just (ac, exchange, subscriber) -> Just $ "(" ++ ac ++ ") " ++ exchange ++ "-" ++ subscriber
28+
Nothing -> Nothing

exercises/phone-number/phone-number_test.hs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,51 @@ main = exitProperly $ runTestTT $ TestList
1919
numberTests :: [Test]
2020
numberTests =
2121
[ testCase "cleans number" $
22-
"1234567890" @=? number "(123) 456-7890"
22+
Just "1234567890" @=? number "(123) 456-7890"
2323
, testCase "cleans another number" $
24-
"6125551212" @=? number "(612) 555-1212"
24+
Just "6125551212" @=? number "(612) 555-1212"
2525
, testCase "cleans number with dots" $
26-
"1234567890" @=? number "123.456.7890"
26+
Just "1234567890" @=? number "123.456.7890"
2727
, testCase "cleans another number with dots" $
28-
"9187654321" @=? number "918.765.4321"
28+
Just "9187654321" @=? number "918.765.4321"
2929
, testCase "valid when 11 digits and first is 1" $
30-
"2468013579" @=? number "12468013579"
30+
Just "2468013579" @=? number "12468013579"
3131
, testCase "invalid when 11 digits" $
32-
"0000000000" @=? number "21234567890"
32+
Nothing @=? number "21234567890"
3333
, testCase "invalid when 9 digits" $
34-
"0000000000" @=? number "123456789"
34+
Nothing @=? number "123456789"
3535
, testCase "invalid when 12 digits" $
36-
"0000000000" @=? number "123456789012"
36+
Nothing @=? number "123456789012"
3737
, testCase "invalid when empty" $
38-
"0000000000" @=? number ""
38+
Nothing @=? number ""
3939
, testCase "invalid when no digits present" $
40-
"0000000000" @=? number " (-) "
40+
Nothing @=? number " (-) "
4141
, testCase "valid with leading characters" $
42-
"2358132134" @=? number "my number is 235 813 2134"
42+
Just "2358132134" @=? number "my number is 235 813 2134"
4343
, testCase "valid with trailing characters" $
44-
"9876543210" @=? number "987 654 3210 - bob"
44+
Just "9876543210" @=? number "987 654 3210 - bob"
4545
, testCase "valid amidst text and punctuation" $
46-
"4158880000" @=? number "Here it is: 415-888-0000. Thanks!"
46+
Just "4158880000" @=? number "Here it is: 415-888-0000. Thanks!"
4747
]
4848

4949
areaCodeTests :: [Test]
5050
areaCodeTests =
5151
[ testCase "area code" $
52-
"123" @=? areaCode "1234567890"
52+
Just "123" @=? areaCode "1234567890"
5353
, testCase "area code with parentheses" $
54-
"612" @=? areaCode "(612) 555-1212"
54+
Just "612" @=? areaCode "(612) 555-1212"
5555
, testCase "area code with leading characters" $
56-
"235" @=? areaCode "my number is 235 813 2134"
56+
Just "235" @=? areaCode "my number is 235 813 2134"
5757
, testCase "invalid area code" $
58-
"000" @=? areaCode " (-) "
58+
Nothing @=? areaCode " (-) "
5959
]
6060

6161
prettyPrintTests :: [Test]
6262
prettyPrintTests =
6363
[ testCase "pretty print" $
64-
"(123) 456-7890" @=? prettyPrint "1234567890"
64+
Just "(123) 456-7890" @=? prettyPrint "1234567890"
6565
, testCase "pretty print with full US phone number" $
66-
"(234) 567-8901" @=? prettyPrint "12345678901"
66+
Just "(234) 567-8901" @=? prettyPrint "12345678901"
6767
, testCase "pretty print amidst text and punctuation" $
68-
"(415) 888-0000" @=? prettyPrint "Here it is: 415-888-0000. Thanks!"
68+
Just "(415) 888-0000" @=? prettyPrint "Here it is: 415-888-0000. Thanks!"
6969
]

0 commit comments

Comments
 (0)