4
main :: IO ()
main = do 
    let a = ("teeeeeeeeeeeeest","teeeeeeeeeeeest")
    b <- app a
    print b

应用程序期望 (bytestring,bytestring) 而不是 ([char],[char]) 我该如何转换它?

4

2 回答 2

10

如果您只包含 ASCII 值或者您只对每个 s 的最后八位感兴趣,您可以将Strings 转换为ByteStrings with Data.ByteString.Char8.pack(或其惰性ByteString版本),StringChar

import qualified Data.ByteString.Char8 as C
main :: IO ()
main = do 
    let a = ("teeeeeeeeeeeeest","teeeeeeeeeeeest")
    b <- app $ (\(x,y) -> (C.pack x, C.pack y)) a
    print b

如果您String包含非 ASCIIChar并且您不仅对最后八位感兴趣,那么您将需要一些其他编码,例如Data.ByteString.UTF8.fromString.

于 2012-07-27T10:00:40.097 回答
6

你可以试试:

import qualified Data.ByteString.Char8 as B --to prevent name clash with Prelude
B.pack "Hello, world"

在这里可以找到很多有用的功能:

http://www.haskell.org/ghc/docs/latest/html/libraries/bytestring/Data-ByteString-Char8.html

你也可以Data.ByteString.Lazy.Char8 用于惰性字节串

http://hackage.haskell.org/packages/archive/bytestring/latest/doc/html/Data-ByteString-Lazy-Char8.html#v:pack

于 2012-07-27T10:08:12.983 回答