Print和putStrLn在Haskell中的区别 [英] Difference between Print and putStrLn in Haskell

查看:575
本文介绍了Print和putStrLn在Haskell中的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑。我尝试使用 print ,但我知道有人应用 putStrLn

  print $ function 
putStrLn $ function
putStrLn
函数需要一个<$ c $ <$ pre $ b $ 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)

这正是 print 函数是。


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

 print $ function 
 putStrLn $ function

解决方案

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.

这篇关于Print和putStrLn在Haskell中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆