Skip to content

Instantly share code, notes, and snippets.

@motokiee
Created October 19, 2015 04:54
Show Gist options
  • Save motokiee/12e5c21e67f6f2692d6e to your computer and use it in GitHub Desktop.
Save motokiee/12e5c21e67f6f2692d6e to your computer and use it in GitHub Desktop.
モジュールとかMaybe使って色々遊ぶ的なやつ #CodePiece
import Data.List
import Data.Char
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..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment