patternMinor
Count occurrences of an element in a list
Viewed 0 times
countlistelementoccurrences
Problem
I've been learning Haskell from LYAH for quite some time, and as a first practice for Chapter 7: Modules, I decided to make a simple
Here's my working code:
Personally, I think I take the cake for using Haskell features, but I also think that the intent of this code is hard to understand. One of my major gripes with it is that it's hard to tell what the
I would greatly appreciate any help in improving the readability of this code.
numTimesFound function: it returns the number of times an element is found in a list.Here's my working code:
numTimesFound :: Ord a => a -> [a] -> Integer
numTimesFound _ [] = 0
numTimesFound x list
| x `notElem` list = 0
| otherwise =
let
appearencesTable = map (\full@(first:_) -> (first, length full + 1)) . group . sort $ list
appearencesOfX = snd $ head $ filter (\(elem, _) -> elem == x) appearencesTable
in
toInteger appearencesOfXPersonally, I think I take the cake for using Haskell features, but I also think that the intent of this code is hard to understand. One of my major gripes with it is that it's hard to tell what the
appearencesTable actually is from the code - it's a list of tuples that gives you the number of times each element was found. For example, for the list [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5] the appearencesTable is [(1, 3), (2, 2), (3, 4), (4, 2), (5, 1)]. If you know that it's pretty easy to get what the rest of the code is trying to do, but it still does require quite a bit of study for such a trivial function. I would greatly appreciate any help in improving the readability of this code.
Solution
Since your type a is required to be an Ord you can access Eq functions. This makes counting occurrences a bit simpler, because you can just filter and count the matching elements:
Of course we don't need to stop there. We can use function composition and
In addition to that 200_success raises an interesting point in the comments:
Actually, there is no reason to require
numTimesFound :: Ord a => a -> [a] -> Integer
numTimesFound _ [] = 0
numTimesFound x list = sum $ map (\a -> 1) $ filter (== x) listOf course we don't need to stop there. We can use function composition and
length to write the last line as:numTimesFound x xs = (length . filter (== x)) xsIn addition to that 200_success raises an interesting point in the comments:
Actually, there is no reason to require
Ord a, when Eq a will doCode Snippets
numTimesFound :: Ord a => a -> [a] -> Integer
numTimesFound _ [] = 0
numTimesFound x list = sum $ map (\a -> 1) $ filter (== x) listnumTimesFound x xs = (length . filter (== x)) xsContext
StackExchange Code Review Q#139587, answer score: 8
Revisions (0)
No revisions yet.