Haskell如何将Char转换成Word8 [英] Haskell How to convert Char to Word8
问题描述
我想将 ByteString
分割为如下所示的单词:
将合格的Data.ByteString导入为BS
main = do
input< - BS.getLine
let xs = BS.split''input
但GHC似乎无法将字符文字本身转换为 Word8
,所以我得到了:
无法匹配预期类型`GHC.Word.Word8'
,实际类型为` char'
在'BS.split'的第一个参数中,即''
在表达式:BS.split''中输入
Hoogle没有发现任何类型签名为 Char - > Word8
和 Word.Word8''
是无效的类型构造函数。如何解决它的任何想法? Data.ByteString.Char8 模块允许您将 Word8 <字节串中的code>值作为
Char
。只需
将限定的Data.ByteString.Char8导入为C
然后参考例如 C.split 。它是相同的字节串,但为方便的字节/ ASCII解析提供了 Char
函数。
I want to split ByteString
to words like so:
import qualified Data.ByteString as BS
main = do
input <- BS.getLine
let xs = BS.split ' ' input
But it appears that GHC can't convert a character literal to Word8
by itself, so I got:
Couldn't match expected type `GHC.Word.Word8'
with actual type `Char'
In the first argument of `BS.split', namely ' '
In the expression: BS.split ' ' input
Hoogle doesn't find anything with type signature of Char -> Word8
and Word.Word8 ' '
is invalid type constructor. Any ideas on how to fix it?
The Data.ByteString.Char8 module allows you to treat Word8
values in the bytestrings as Char
. Just
import qualified Data.ByteString.Char8 as C
then refer to e.g. C.split. It's the same bytestring under the hood, but the Char
-oriented functions are provided for convenient byte/ascii parsing.
这篇关于Haskell如何将Char转换成Word8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!