Search code examples
stringhaskellbytestring

ByteStrings and Strings


How to solve following problem:

import Data.ByteString.Lazy as BS  (readFile, ByteString, unpack, fromStrict)
import Data.ByteString.Char8 as C8 (pack)
import Data.ByteString.UTF8        (toString)
import Data.Char                   (chr)

stringToBS :: String -> BS.ByteString
stringToBS str = BS.fromStrict $ C8.pack str

recode :: String -> String
recode str = toString $ urlDecode True (stringToBS str)

NOTE I need to have them the types I set already.

Error at compiling:

Couldn't match expected type ‘Data.ByteString.Internal.ByteString’
            with actual type ‘ByteString’
NB: ‘Data.ByteString.Internal.ByteString’
      is defined in ‘Data.ByteString.Internal’
    ‘ByteString’ is defined in ‘Data.ByteString.Lazy.Internal’
In the second argument of ‘urlDecode’, namely ‘(stringToBS str)’
In the second argument of ‘($)’, namely
  ‘urlDecode True (stringToBS str)’

How can I solve this mistake?


Solution

  • I wasn't able to find a urlDecode in Hackage which fits your usage so I can't be sure., but it probably expects a strict ByteString rather than a lazy one, in which case the following should work.

    import Data.ByteString as BS  (readFile, ByteString, unpack)
    import Data.ByteString.Char8 as C8 (pack)
    import Data.ByteString.UTF8        (toString)
    import Data.Char                   (chr)
    
    recode :: String -> String
    recode str = toString $ urlDecode True (C8.pack str)