[Haskell-beginners] As pattern, @

Lawrence Bottorff borgauf at gmail.com
Wed Jan 27 06:01:23 UTC 2021


I'm looking at this <https://wiki.haskell.org/99_questions/Solutions/8> and
wondering how the "as pattern" actually is working

compress (x:ys@(y:_))
    | x == y    = compress ys
    | otherwise = x : compress ys
compress ys = ys

I'm sure it's just some version of my stab at eliminating consecutive
duplicates in a list

compress :: Eq a => [a] -> [a]
compress [] = []
compress [x] = [x]
compress (x:y:xs) = if x == y then compress (y:xs) else x : compress (y:xs)

only smarter. Could someone walk me through the (x:ys@(y:_)) part?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210127/b893147d/attachment.html>


More information about the Beginners mailing list