Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Data.List
wordNums :: String -> [(String,Int)]
wordNums = map (\ws -> (head ws, length ws)) . group . sort . words
-- 関数合成がないと読みづらい
wordNums' xs = map (\ws -> (head ws,length ws)) (group (sort (words xs)))
main = do
print $ words "hey these are the words in this sentence"
print $ group [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7]
print $ group ["boom","bip","bip","boom","boom"]
print $ sort [5,4,3,7,2,1]
print $ sort ["boom","bip","bip","boom","boom"]
print $ wordNums "wa wa wee wa"
print $ wordNums' "wa wa wee wa"
-- ["hey","these","are","the","words","in","this","sentence"]
-- [[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[7]]
-- [["boom"],["bip","bip"],["boom","boom"]]
-- [1,2,3,4,5,7]
-- ["bip","bip","boom","boom","boom"]
-- [("wa",3),("wee",1)]
-- [("wa",3),("wee",1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.