by shigemk2

当面は技術的なことしか書かない

標準モジュールの関数で問題を解く

標準

import Data.List
import Data.Char
-- import qualified Data.Map as M

wordNums :: String -> [(String,Int)]
wordNums = map (\ws -> (head ws, length ws)) . group. sort . words

isIn :: (Eq a) => [a] -> [a] -> Bool
needle `isIn` haystack = any (needle `isPrefixOf`) (tails haystack)

encode :: Int -> String -> String
encode offset msg = map (\c -> chr $ ord c + offset) msg

decode :: Int -> String -> String
decode shift msg = encode (negate shift) msg

digitSum :: Int -> Int
digitSum = sum . map digitToInt . show

firstTo40 :: Maybe Int
firstTo40 = find (\x -> digitSum x == 40) [1..]

firstTo :: Int -> Maybe Int
firstTo n = find (\x -> digitSum x == n) [1..]
*Main> words "hey these are the words in this sentense"
["hey","these","are","the","words","in","this","sentense"]
*Main> group [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7]
[[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[7]]
*Main> group ["boom", "bip", "bip", "boom", "boom"]
[["boom"],["bip","bip"],["boom","boom"]]
*Main> wordNums "hoge hoge hoge"
[("hoge",3)]
*Main> foldl (+) 0 (replicate 100 1)
100
*Main> foldl (+) 0 (replicate 1000000 1)
1000000
*Main> foldl (+) 0 (replicate 5000000 1)
5000000
*Main> foldl (+) 0 (replicate 10000000 1)
10000000
*Main> foldl (+) 0 (replicate 50000000 1)
*** Exception: stack overflow
*Main> foldl' (+) 0 (replicate 50000000 1)
50000000
*Main> digitToInt '2'
2
*Main> digitToInt 'F'
15
*Main> digitToInt 'z'
*** Exception: Char.digitToInt: not a digit 'z'
*Main> :t find
find :: (a -> Bool) -> [a] -> Maybe a
*Main> Nothing
Nothing
*Main> Just "hey"
Just "hey"
*Main> Just 3
Just 3
*Main> :t Just "hey"
Just "hey" :: Maybe [Char]
*Main> :t Just True
Just True :: Maybe Bool
*Main> find (>4) [3,4,5,6,7]
Just 5
*Main> find odd [2,4,6,8,9]
Just 9
*Main> find (=='z') "mjolnir"
Nothing
*Main> firstTo40
Just 49999
*Main> firstTo 27
Just 999
*Main> firstTo 50
Just 599999
*Main> firstTo 60
Just 6999999