我正在尝试将一个整数读取为Text:
-- This works
mm x = do
T.putStrLn "Testing x being input as Text"
T.putStrLn x
-- y <- x * 2
-- putStrLn y
mm "10"
生产
Testing x being input as Text
10
太好了。如果以Text开头,则可以使用。很好。
但是
-- This does not work
mm2 x = do
T.putStrLn "Testing x being input as an integer and converted to Text"
let m = tshow x
T.putStrLn m
-- y <- x * 2
-- putStrLn y
mm2 10
<interactive>:3:17: error:
• Variable not in scope: tshow :: p -> t
• Perhaps you meant ‘show’ (imported from Prelude)
从here我了解到tshow
:
tshow :: Show a => a -> Text
应该存在于Prelude
中,并且应该将其转换为文本。
常规(非常规)show
也会失败:
-- This does not work
mm2 x = do
T.putStrLn "Testing x being input as an integer and converted to Text"
let m = show x
T.putStrLn m
-- y <- x * 2
-- putStrLn y
mm2 10
使用
<interactive>:4:20: error:
• Couldn't match type ‘String’ with ‘Text’
Expected type: Text
Actual type: String
• In the first argument of ‘Data.Text.IO.putStrLn’, namely ‘m’
In a stmt of a 'do' block: Data.Text.IO.putStrLn m
In the expression:
do Data.Text.IO.putStrLn "Testing x being input as an integer and converted to Text"
let m = show x
Data.Text.IO.putStrLn m
也是
-- This does not work
mm2 x = do
T.putStrLn "Testing x being input as an integer and converted to Text"
let m = T.pack x
T.putStrLn m
-- y <- x * 2
-- putStrLn y
mm2 10
使用
<interactive>:1:5: error:
• No instance for (Num String) arising from the literal ‘10’
• In the first argument of ‘mm2’, namely ‘10’
In the expression: mm2 10
In an equation for ‘it’: it = mm2 10
我如何阅读/显示/打包/投射一个整数作为文本?很难做到我在做什么错了?
还有
-- This does not work
import qualified Data.Text.IO as T
import qualified Data.Text as TS
mm2 x = do
T.putStrLn "Testing x being input as an integer and converted to Text"
let m = T.tshow x
T.putStrLn m
-- y <- x * 2
-- putStrLn y
mm2 10
生产
<interactive>:3:17: error:
Not in scope: ‘T.tshow’
No module named ‘T’ is imported.
还有
mm2 x = do
T.putStrLn "Testing x being input as an integer and converted to Text"
let m = TS.tshow x
T.putStrLn m
-- y <- x * 2
-- putStrLn y
mm2 10
产生
<interactive>:3:17: error:
Not in scope: ‘TS.tshow’
No module named ‘TS’ is imported.
答案 0 :(得分:0)
我仍然不清楚tshow
的位置以及为什么它出现在Hoogle上,但是无法从Prelude
,Data.Text
或Data.Text.IO
导入。
但这会正确执行:
mm2 x = do
T.putStrLn "Testing x being input as an integer and converted to Text"
let m = T.pack $ show x
T.putStrLn m
-- y <- x * 2
-- putStrLn y
mm2 10
首先从show
到Prelude
,然后从Integer -> String
到T.pack
。