Skip to content

Instantly share code, notes, and snippets.

@ToJans
Forked from mathiasverraes/phonenumbers.hs
Last active September 11, 2015 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToJans/90553be706c797d6ec0e to your computer and use it in GitHub Desktop.
Save ToJans/90553be706c797d6ec0e to your computer and use it in GitHub Desktop.
Phone Number Kata
-- 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