ByteStrings和Strings

时间:2016-05-09 22:24:33

标签: string haskell bytestring

如何解决以下问题:

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)

注意我需要将它们设置为我已设置的类型。

编译时出错:

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)’

我该如何解决这个错误?

1 个答案:

答案 0 :(得分:2)

我无法在Hackage中找到适合您使用的urlDecode因此我无法确定。但它可能需要严格的ByteString而不是懒惰的,其中以下情况应该有效。

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)