Haskell Bytestrings: How to pattern match?

前端 未结 5 579
悲&欢浪女
悲&欢浪女 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:03

    Just to address the error message you received and what it means:

    Couldn't match expected type `BS.ByteString'
           against inferred type `[a]'
    In the pattern: []
    In the definition of `dropR': dropR [] = []
    

    So the compiler expected your function to be of type: BS.ByteString -> BS.ByteString because you gave it that type in your signature. Yet it inferred (by looking at the body of your function) that the function is actually of type [a] -> [a]. There is a mismatch there so the compiler complains.

    The trouble is you are thinking of (:) and [] as syntactic sugar, when they are actually just the constructors for the list type (which is VERY different from ByteString).

提交回复
热议问题