Created
December 29, 2011 05:26
-
-
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
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
-- 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