[英]Haskell + output String as type String
我写了一个基本的递归函数:
bibliography_rec :: [(String, String, Int)] -> String
bibliography_rec [] = ""
bibliography_rec (x:xs) = (citeBook x) ++ "\n" ++ (bibliography_rec xs)
citeBook
仅将元组重新格式化为字符串。
使用此输入运行时:
ghci> bibliography_rec [("Herman Melville", "Moby Dick", 1851),("Georgy Poo", "Alex Janakos", 1666)]
它产生:
"Moby Dick (Herman Melville, 1851)\nAlex Janakos (Georgy Poo, 1666)\n"
我需要逐行打印,因此我使用了以下方法:
bibliography_rec (x:xs) = putStr ((citeBook x) ++ "\n" ++ (bibliography_rec xs))
我的问题是我的输出NEEDS的类型为String
NOT IO ()
我在这个问题上停留了太久了,所以任何帮助都很棒!
看起来您已经在那儿了,您只需要putStrLn
字符串putStrLn
而不是print
它(这是ghci的默认设置)。 print
通过show
首先运行其参数,因此它将引用转义字符,例如"\\n"
。
ghci> putStrLn $ bibliography_rec [...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.