写给 stdout
根据 Haskell 2010 语言规范 ,以下是 Prelude 中可用的标准 IO 函数,因此不需要导入它们。
putChar::Char -> IO () - 将 char 写入 stdout
Prelude> putChar 'a'
aPrelude>  -- Note, no new line
putStr::String -> IO () - 将 String 写入 stdout
Prelude> putStr "This is a string!"
This is a string!Prelude>  -- Note, no new line
putStrLn::String -> IO () - 将 String 写入 stdout 并添加一个新行
Prelude> putStrLn "Hi there, this is another String!"
Hi there, this is another String!
print::Show a => a -> IO () - 将 a 的一个实例写入 stdout
Prelude> print "hi"
"hi"
Prelude> print 1
1
Prelude> print 'a'
'a'
Prelude> print (Just 'a')  -- Maybe is an instance of the `Show` type class
Just 'a'
Prelude> print Nothing
Nothing
回想一下,你可以使用 deriving 为你自己的类型实例化 Show:
-- In ex.hs
data Person = Person { name::String } deriving Show
main = print (Person "Alex")  -- Person is an instance of `Show`, thanks to `deriving`
在 GHCi 中加载和运行:
Prelude> :load ex.hs
[1 of 1] Compiling ex             ( ex.hs, interpreted )
Ok, modules loaded: ex.
*Main> main  -- from ex.hs
Person {name = "Alex"}
*Main>