Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Question of the Day
- -- Given a phone number, write a function that outputs every
- -- possible letter combination for that number.
- -- First, we can make an auxiliary function
- -- that will take a number and convert it
- -- to its corresponding telephone numpad.
- -- This function is done via recursion and concatenation with the cons operator.
- phoneToString :: Integer -> [String]
- phoneToString 0 = []
- phoneToString n = (numPick (n `mod` 10)):(phoneToString $ fromIntegral $ floor ((fromIntegral n)/10) )
- where numPick d = case d of 1 -> " "
- 2 -> "abc"
- 3 -> "def"
- 4 -> "ghi"
- 5 -> "jkl"
- 6 -> "mno"
- 7 -> "pqrs"
- 8 -> "tuv"
- 9 -> "wxyz"
- 0 -> " "
- -- Then we do a quick n'dirty list comprehension.
- -- Just a note that the earlier function actually
- -- has the list in reverse because the least sig. num
- -- has the most precedence, so we use reverse to get it
- -- to the correct order.
- phoneNumberMapper :: Integer -> [String]
- phoneNumberMapper num
- | ( (num < 10^6) || (num > 10^7) ) = ["INVALID"] -- Assuming that a phone number is only 7-digits.
- | otherwise = [ [d0]++[d1]++[d2]++[d3]++[d4]++[d5]++[d6] |
- d0 <- (numList !! 0),
- d1 <- (numList !! 1),
- d2 <- (numList !! 2),
- d3 <- (numList !! 3),
- d4 <- (numList !! 4),
- d5 <- (numList !! 5),
- d6 <- (numList !! 6)
- ]
- where numList = reverse $ phoneToString num
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement