【发布时间】:2021-06-25 06:49:08
【问题描述】:
我有两个 int 变量,想将它们打印在一行上,用空格分隔。如何在 Haskell 中实现这一点?
main :: IO ()
main = do
let one = 1
let two = 2
print ( one, two ) -- 1 option
mapM_ (putStr . show) [one, two] -- 2 option
print (one ++ " " ++ two) -- 3 option
1 选项给出结果:(1,2)
2 选项给出结果:12
3 选项给出错误:
No instance for (Num [Char]) arising from the literal '2'
那么如何在一行中打印两个值,用空格分隔?
【问题讨论】:
-
嗨 - 我知道这很令人困惑,因为大多数其他语言都使用
print但你应该尽量避免这种情况(使用putStrLn代替)因为它总是会添加一个show这通常是不是你想要的 - 使用putStrLn会迫使你考虑如何格式化(因为它只适用于Strings)
标签: variables haskell printing io output