Haskell Bytestrings: How to pattern match?

前端 未结 5 604
悲&欢浪女
悲&欢浪女 2020-12-28 14:23

I\'m a Haskell newbie, and having a bit of trouble figuring out how to pattern match a ByteString. The [Char] version of my function looks like:

5条回答
  •  一向
    一向 (楼主)
    2020-12-28 15:10

    You can use view patterns for such things

    {-# LANGUAGE ViewPatterns #-}    
    import Data.ByteString (ByteString, cons, uncons, singleton, empty)
    import Data.ByteString.Internal (c2w) 
    
    dropR :: ByteString -> ByteString
    dropR (uncons -> Nothing) = empty
    dropR (uncons -> Just (x,uncons -> Nothing)) = singleton x
    dropR (uncons -> Just (x,uncons -> Just(y,xs))) =
        if x == c2w 'a' && y == c2w 'b'
        then dropR xs
        else cons x (dropR $ cons y xs)
    

提交回复
热议问题