【发布时间】:2012-01-16 23:25:52
【问题描述】:
我在尝试读取 ByteString 时总是遇到以下错误:Prelude.read: no parse
以下是在浏览器中呈现时会导致此错误发生的代码示例:
factSplice :: SnapletSplice App App
factSplice = do
mbstr <- getParam "input" -- returns user input as bytestring
let str = maybe (error "splice") show mbstr
let n = read str :: Int
return [X.TextNode $ T.pack $ show $ product [1..n]]
或者更简单:
simple bs = read (show bs) :: Int
由于某种原因,show bs 之后的结果字符串包含引号。
因此,为了解决该错误,我必须删除引号,然后删除 read 它。
我使用从互联网复制的以下功能来做到这一点:
sq :: String -> String
sq s@[c] = s
sq ('"':s) | last s == '"' = init s
| otherwise = s
sq ('\'':s) | last s == '\'' = init s
| otherwise = s
sq s = s
然后simple bs = read (sq.show bs) :: Int 按预期工作。
- 为什么会这样?
- 将 ByteString 转换为 Int 的最佳方法是什么?
【问题讨论】:
标签: haskell bytestring