praxis

Various programming exercises.
git clone git://git.jtobin.io/praxis.git
Log | Files | Refs

FavoriteColor.hs (761B)


      1 {-# OPTIONS_GHC -Wall #-}
      2 
      3 module Main where
      4 
      5 import Control.Arrow
      6 import Data.Function
      7 import Data.List
      8 import System.Environment
      9 
     10 parse :: String -> String
     11 parse input = fst $ maximumBy (compare `on` snd) colors where
     12   recs   = filter ((== "favoritecolor") . fst) . fmap record $ lines input
     13   colors = fmap ((head . nub) &&& length) . group . sort $ fmap snd recs
     14 
     15 record :: String -> (String, String)
     16 record rec = (key, val) where
     17   alphaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
     18   key = takeWhile (/= ':') rec
     19   val = dropWhile (`notElem` alphaNum) . dropWhile (/= ':') $ rec
     20 
     21 main :: IO ()
     22 main = getArgs >>= \args -> case args of
     23   [] -> putStrLn "USAGE: ./favorite <FILE>"
     24   (file:_) -> readFile file >>= putStrLn . parse
     25