【发布时间】:2015-11-05 18:50:49
【问题描述】:
imP:: Int -> IO String
imP n = do
x <- getLine
if n >= 0 then return ( (concat (replicate n " ")) ++ fun1 x) else return ( fun2 n x)
where
fun1 [] = ""
fun1 (x:xs)
| isAlpha x = [x] ++ fun1 xs
| otherwise = "\n" ++ fun1 xs
fun2 n [] = ""
fun2 n (x:xs)
| isAlpha x = [x] ++ fun2 n xs
| otherwise = "\n" ++ (concat (replicate (abs n) " ")) ++ fun2 n xs
我有这个代码。并给 getLine 一个输入“hello3mello”,它返回:
"hello\nmello"
但我需要:
"hello
mello"
编辑:
<interactive>:32:9:
Couldn't match type `IO String' with `[Char]'
Expected type: String
Actual type: IO String
In the first argument of `putStr', namely `(imP 3)'
In the expression: putStr (imP 3)
【问题讨论】:
-
如何打印?
-
使用 "putStr" 而不是 "print" 你会得到正确的
-
如果你想打印出“hello mello”,请使用:putStr "\"hello\nmello\""
-
我编辑了第一篇文章。使用 putStr 或 print 或 putStrLn 时出现该错误。