-
-
Save ToJans/90553be706c797d6ec0e to your computer and use it in GitHub Desktop.
Phone Number Kata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Given a list of phone numbers, determine if it is consistent. | |
-- In a consistent phone list no number is a prefix of another. For example: | |
-- Bob 91 12 54 26 | |
-- Alice 97 625 992 | |
-- Emergency 911 | |
-- In this case, it is not possible to call Bob because the phone exchange | |
-- would direct your call to the emergency line as soon as you dialled the | |
-- first three digits of Bob's phone number. So this list would not be consistent. | |
module PhoneList(isConsistent) where | |
-- Rule: | |
-- A phonelist is consistent if the phonelist contains none of that phonelist's prefixes | |
isConsistent :: [String] -> Bool | |
isConsistent phoneList = not $ any phoneListContainsPrefix phoneList where | |
phoneListContainsPrefix prefix = any (prefix `isPrefixOf `) phoneList where | |
isPrefixOf [] [] = False | |
isPrefixOf [] _ = True | |
isPrefixOf _ [] = False | |
isPrefixOf (x:xs)(y:ys) = x == y && xs `isPrefixOf` ys | |
-- *PhoneList> isConsistent ["911","1123456464","912111"] | |
-- True | |
-- *PhoneList> isConsistent ["911","1123456464","91111"] | |
-- False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment