Print和putStrLn在Haskell中的区别 [英] Difference between Print and putStrLn in Haskell
问题描述
print
,但我知道有人应用 putStrLn
。 print $ function
函数需要一个<$ c $ <$ pre $ b $
putStrLn $ function
$ cputStrLn String
并将其显示在屏幕上,后面跟着一个换行符( put str ,后跟一个新的 L i n e)。
因为它只适用于
字符串
s,一个常见的习惯用法是将任何对象转换为String
,然后将putStrLn
应用于显示它。将对象转换为String
的通用方法是使用show
函数,因此您的代码最终会以很多putStrLn(显示1)
putStrLn(显示[1,2,3])
putStrLn(show(Just 42))
一旦您注意到了,定义并不是很大一个将
字符串
转换为一个字符串的函数print x = putStrLn(show x)
这正是
I am confused. I try to use
putStrLn
. What are the real differences between them?print $ function putStrLn $ function
解决方案The function
putStrLn
takes aString
and displays it to the screen, followed by a newline character (put a String followed by a new Line).Because it only works with
String
s, a common idiom is to take any object, convert it to aString
, and then applyputStrLn
to display it. The generic way to convert an object to aString
is with theshow
function, so your code would end up with a lot ofputStrLn (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 stepprint x = putStrLn (show x)
which is exactly what the
这篇关于Print和putStrLn在Haskell中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!