Return list with numbers of color occurrences in another list
I think you're tripping yourself up by position-encoding the colors. Position-encoding is usually a code smell in Haskell, what you probably want is an association. Consider the difference between [Int] and [(Peg, Int)], and just how much more expressive of intent the latter is.

(You could be even more specific at the type level with something like type Count = Int countColors :: Code -&gt; Map Peg Count but that's probably excessive for a small game.)

So then how do you write a function with type Code -&gt; [(Peg, Int)]? As you noticed the [Peg] may be in any order, so to compensate you can filter for each individual color, or you could leverage the Ord Peg instance and sort the original list to group identical colors together. After the colors are in order, what's left is to simply run-length encode the list.

countColors :: Code -&gt; [(Peg, Int)]
countColors = map (\shade -&gt; (head shade, length shade)) . group . sort