I'm solving "mastermind game" problem from CIS haskell course. Here is the link to assignment pdf if someone is interested.
List of possible colors is defined like this:
data Peg = Red | Green | Blue | Yellow | Orange | Purple deriving (Show, Eq, Ord) type Code = [Peg] colors :: [Peg] colors = [Red, Green, Blue, Yellow, Orange, Purple]
And I had to write a function that will take a list with color guesses and return the occurrence count for each available color in the form of a list.
countColors :: Code -> [Int] countColors a = map (\(x,y) -> length $ filter (x==) y) combo where combo = zip colors (take 6 $ repeat a)
It works fine but I'd like to know if there is better/more expressive way to write it (using most basic available functions).
I'm not sure what's good practice in Haskell; that is my main problem. I have tried to write in point free notation at first but it looked overly complicated.
My worry is that code is not clear enough. I like to have code that is as obvious as possible.
Here are test cases in case it helps:
countColors [Red, Blue, Yellow, Purple] == [1, 0, 1, 1, 0, 1] countColors [Green, Blue, Green, Orange] == [0, 2, 1, 0, 1, 0]