Difference between print and putStrLn in Haskell

天大地大妈咪最大 提交于 2019-11-28 18:22:24

问题


I am confused. I tried to use print, but I know people apply putStrLn. What are the real differences between them?

print $ function 
putStrLn $ function

回答1:


The function putStrLn takes a String and displays it to the screen, followed by a newline character (put a String followed by a new Line).

Because it only works with Strings, a common idiom is to take any object, convert it to a String, and then apply putStrLn to display it. The generic way to convert an object to a String is with the show function, so your code would end up with a lot of

putStrLn (show 1)
putStrLn (show [1, 2, 3])
putStrLn (show (Just 42))

Once you notice that, it's not a very big stretch to define a function that converts to a String and displays the string in one step

print x = putStrLn (show x)

which is exactly what the print function is.



来源:https://stackoverflow.com/questions/19288652/difference-between-print-and-putstrln-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!