我如何打印特殊字符,例如列表或新线


[m@green09 ~]$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> print "asdf"
"asdf"
Prelude> print "tasdf"
"tasdf"
Prelude> 

很棒。太棒了。

如何打印制表或newline?

您只是这样做。

> putStrLn "atb"
a   b

当您将类型Show a => a的值插入repl中时,GHCI将在其上执行print,如果此值的类型为IO (),则只会对其进行评估。如果您查看打印的定义,您将看到print = putStrLn . show或更少的点:print s = putStrLn (show s)

问题是show不是漂亮打印的工具。此功能的目的是"显示"基础结构,以便可以由Haskell或人类回到read。这是揭示逃脱角色的点。您可以检查show "t" /= "t"

如果要实际打印字符串"为",则应明确调用putStrputStrLn操作,以省略此show层。


几个示例:

>>> putStrLn "atb"
a    b
>>> "atb"
"atb"
>>> show "atb" -- notice that it is actually `putStrLn (show (show "atb"))`!
""a\tb""
>>> print "atb"
"atb"

最新更新