Created
June 14, 2014 06:24
-
-
Save mmitou/d7b3f212483dee39c3e9 to your computer and use it in GitHub Desktop.
単語数比較
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "code.google.com/p/go-tour/wc" | |
| "strings" | |
| ) | |
| func WordCount(s string) map[string]int { | |
| var wc map[string]int = make(map[string]int) | |
| for _, key := range strings.Fields(s) { | |
| count, hasKey := wc[key] | |
| if hasKey { | |
| wc[key] = count + 1 | |
| } else { | |
| wc[key] = 1 | |
| } | |
| } | |
| return wc | |
| } | |
| func main() { | |
| wc.Test(WordCount) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| wordCount :: String -> [(String, Int)] | |
| wordCount s = foldl count' [] $ words s | |
| count' :: [(String, Int)] -> String -> [(String, Int)] | |
| count' [] w = [(w, 1)] | |
| count' ((v, n):xs) w | v == w = (v, n+1) : xs | |
| | otherwise = (v, n) : count' xs w |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment