Skip to content

Instantly share code, notes, and snippets.

@sczizzo
Created December 29, 2011 05:26
Show Gist options
  • Save sczizzo/1532113 to your computer and use it in GitHub Desktop.
Save sczizzo/1532113 to your computer and use it in GitHub Desktop.
Return the number of tuples in a list whose first member is prefixed by some character
-- Return the number of tuples in a list whose first member is prefixed by a
foo :: Char -> [(String, a)] -> Int
foo a = foldl succIfPrefixed 0 where
succIfPrefixed acc (x:xs, y) | x == a = succ acc
| otherwise = acc
-- Or more compactly...
foo' :: Char -> [(String, a)] -> Int
foo' a = foldl (\acc (x:xs, y) -> if x == a then acc + 1 else acc) 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment