Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

newline - Haskell new line not working

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---------- Cast: Daniel Craig, Judi Dench, Ralph Fiennes Year: 2012 Fans: 7 "

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


displayFilm :: Film -> String
displayFilm (title, cast, year, fans) = 
    title ++ "----------" ++
    "
 Cast: " ++ (displayList cast) ++
    "
 Year: " ++ (show year) ++
    "
 Fans: " ++ show (length fans) ++ "
"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...