'How do you detect if a list has exactly 3 items in Haskell?

I have this but I got an error:

-- test if a list contains exactly three characters
test :: [Char] -> Bool
test xs   | [_ , _ , _] = True
          | otherwise = False


Solution 1:[1]

I'd write something like

hasLength3 :: [a] -> Bool
hasLength3 xs = xs `hasLength` 3

hasLength :: [a] -> Int -> Bool
hasLength = foldr go stop
  where
    go _ r n = n > 0 && r (n - 1)
    stop n = n == 0

Solution 2:[2]

main = do
  a:b:c:[] <- pure "123"  -- fails in runtime in MonadFail if they are not exactly 3
  print a >> print b >> print c

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dfeuer
Solution 2 RandomB