Haskell新行不起作用 [英] Haskell new line not working

查看:96
本文介绍了Haskell新行不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我进入的内容,大约20分钟左右,现在试图让新线路工作,但它总是在GHCI中显示为单线。 GHCi:

  displayFilm(Skyfall,[Daniel Craig,Judi Dench,Ralph Fiennes],2012 ,[Bill,Olga,Zoe,Paula,Megan,Sam,Wally])

以下是打印的内容:

Skyfall ---------- \\ \\ n演员:丹尼尔克雷格,朱迪丹奇,拉尔夫费因斯\年:2012 \粉丝:7 \\\

  displayList :: [String]  - >字符串
displayList [] =
displayList [x] = x ++++ displayList []
displayList(x:xs)= x ++,++ displayList xs


displayFilm ::电影 - > String
displayFilm(title,cast,year,fans)=
title ++----------++
\\\
演员:++ (displayList cast)++
\\\
年份:++(show year)++
\\\
粉丝:++ show(length fans)++\\\


解决方案

按原样打印字符串,字符,请使用:

  putStr字符串

  putStrLn字符串

如果你想在最后换行。在您的情况下,您可能正在寻找

  putStr(displayFilm(....))

为什么需要这个?在GHCi中,如果评估表达式 s ,结果将被打印,就像运行 print s 一样(除非它具有类型 IO东西 - 忘记这个特殊情况)。如果 e 是一个字符串, print 转义所有特殊字符并输出结果。这是因为 print 用于输出一个字符串,其语法遵循Haskell表达式中的语法。对于数字,这是通常的十进制符号。对于字符串,我们会得到引号和转义字符。


Been messing around for about 20 minutes now trying to get the new line working however it always shows in GHCI as a single line.

Here is what I enter into GHCi:

displayFilm ("Skyfall",["Daniel Craig", "Judi Dench", "Ralph Fiennes"], 2012, ["Bill", "Olga", "Zoe", "Paula", "Megan", "Sam", "Wally"])

Here is what is printed:

"Skyfall----------\n Cast: Daniel Craig, Judi Dench, Ralph Fiennes\n Year: 2012\n Fans: 7\n"

displayList :: [String] -> String
displayList [] = ""
displayList [x] = x ++ "" ++  displayList []
displayList (x:xs) = x ++ ", " ++ displayList xs


displayFilm :: Film -> String
displayFilm (title, cast, year, fans) = 
    title ++ "----------" ++
    "\n Cast: " ++ (displayList cast) ++
    "\n Year: " ++ (show year) ++
    "\n Fans: " ++ show (length fans) ++ "\n"

解决方案

To print a string as it is, without escaping special characters, use:

putStr string

or

putStrLn string

if you want an extra newline at the end. In you case, you are probably looking for

putStr (displayFilm (....))

Why is this needed? In GHCi, if you evaluate an expression s the result will be printed as if running print s (unless it has type IO something -- forget about this special case). If e is a string, print escapes all the special characters and output the result. This is because print is meant to output a string whose syntax follows the one in Haskell expressions. For numbers, this is the usual decimal notation. For strings, we get quotes and escaped characters.

这篇关于Haskell新行不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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