Skip to content

Instantly share code, notes, and snippets.

@proppy
Created May 18, 2011 13:20
Show Gist options
  • Save proppy/978552 to your computer and use it in GitHub Desktop.
Save proppy/978552 to your computer and use it in GitHub Desktop.
How to count in haskell
count1 c (x:xs) = count1' (c == x) + count1 c xs
count1 c [] = 0
count1' True = 1
count1' False = 0
count2 c (x:xs) | c /= x = 0 + count2 c xs
| otherwise = 1 + count2 c xs
count2 c [] = 0
count3 c (x:xs) = (if c == x then 1 else 0) + count3 c xs
count3 c [] = 0
count4 c xs = sum [1 | x <- xs, x == c]
count5 c xs = length (filter (==c) xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment