Skip to content

Instantly share code, notes, and snippets.

@mmitou
Created June 14, 2014 06:24
Show Gist options
  • Select an option

  • Save mmitou/d7b3f212483dee39c3e9 to your computer and use it in GitHub Desktop.

Select an option

Save mmitou/d7b3f212483dee39c3e9 to your computer and use it in GitHub Desktop.
単語数比較
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)
}
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