How to pass a bytestring to `unpack` funciton in `Data.ByteString.Lazy`?

Hey everyone! I want to pass a bytestring to the unpack function in the module Data.ByteString.Lazy. After I tried few ways I find that I can only obtain a byte string in Haskell by using pack function in the same module and pass a Word8 list to it.

Is there anyway I can directly write a bytestring in Haskell just like I write a normal list [1, 2, 3]?

Edit : According to the book Learn You a Haskell For Great Good, bytestring seems to be represented by :

Chunk "can" Empty

Since we have the following example in the book :

ghci> B.pack [99,97,110]
Chunk "can" Empty

But my ghci prompt shows the following :

λ> B.pack [99,97,110]
"can"
1 Like

What you have is a bytestring.

λ> import Data.ByteString.Lazy as B
λ> a = B.pack [99,97,100]
λ> :t a
a :: ByteString

The Show instance was changed many years ago.


To write a ByteString like you write a normal list, try OverloadedStrings language extension.

λ> import Data.ByteString.Lazy as B
λ> :set -XOverloadedStrings
λ> "Hello, Dendy!" :: B.ByteString
"Hello, Dendy!"
λ> :t it
it :: ByteString

or if you really want to write a list, OverloadedLists:

λ> :set -XOverloadedLists
λ> [99, 97, 110] :: B.ByteString
"can"

Does this answer your question?

10 Likes

Thanks! This is exactly what I want!

1 Like