Skip to content

Instantly share code, notes, and snippets.

@shoooe
Created June 29, 2014 14:24
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 shoooe/174bf8865354e54e7246 to your computer and use it in GitHub Desktop.
Save shoooe/174bf8865354e54e7246 to your computer and use it in GitHub Desktop.
Simple program that calculates the number of possible phone numbers of length 4 that have 2 or more equal digits, and displays them.
import Data.Map (Map)
import qualified Data.Map as Map
countDistinct :: Ord a => [a] -> Int
countDistinct = Map.size . foldr fn Map.empty
where fn e = Map.insertWith (+) e 1
validStrings :: [String]
validStrings =
filter ((`elem` [1..3]) . countDistinct) $ do
a <- ['0'..'9']
b <- ['0'..'9']
c <- ['0'..'9']
d <- ['0'..'9']
return $ a:b:c:d:[]
main :: IO ()
main = do
putStrLn $ "Total: " ++ (show . length) validStrings
putStrLn "List:"
mapM_ (putStrLn . (" "++)) validStrings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment