Problem 22

http://projecteuler.net/index.php?section=problems&id=22

import Data.List

main :: IO ()
main = do names <- readFile "names.txt"
          print $ sum $ zipWith calcScore [1..] $ sort $ divide names

divide:: String -> [String]
divide = unfoldr f
    where f :: String -> Maybe (String, String)
          f str = case (break (== ',') $ str) of
                    ("", "") -> Nothing
                    (a, "")  -> Just ((init . tail) a, "")
                    (a, b)   -> Just ((init . tail) a, tail b)

calcScore:: Int -> String -> Int
calcScore i name = i * (sum $ map (\c -> (fromEnum c) - (fromEnum 'A') + 1) name)
  • }

divide イマイチ、、