I was looking at some Haskell source code and came across a pattern match with !_
, the code is here: http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.List.html#unsafeTake
take n xs | 0 < n = unsafeTake n xs
| otherwise = []
-- A version of take that takes the whole list if it's given an argument less
-- than 1.
{-# NOINLINE [1] unsafeTake #-}
unsafeTake :: Int -> [a] -> [a]
unsafeTake !_ [] = []
unsafeTake 1 (x: _) = [x]
unsafeTake m (x:xs) = x : unsafeTake (m - 1) xs
I don't really understand how the "strict wildcard" works and why it's useful for this function (or any other function).
take
function withundefined
asn
argument then it is unlikely to catch error inunsafeTake
function becausen
is compared with zero earlier, i.e. it would be evaluated earlier. But becausetake
function is supposed to be inlined as much as possible (as comment says) then this check may disappear after some optimizations. That's why we need!_
inunsafeTake
as well. – Augustus