我尝试使用\n
,putStrLn
和print
,但没有任何工作。我如何在Haskell上划线?
当我使用\n
时,结果仅连接字符串,并且当我使用putStrLn
或print
时,我收到一个类型错误。
输出为\n
:
formatLines [("a",12),("b",13),("c",14)]
"a...............12\nb...............13\nc...............14\n"
输出为putStrLn
:
format.hs:6:22:
Couldn't match type `IO()' with `[Char]'
Expected type: String
Actual type: IO()
In the return type of a call of `putStrLn'
In the expression:
putStrLn (formatLine ((fst x), (snd x)) ++ formatLines xs)
In an equation for `formatLines':
formatLines (x : xs)
= putStrLn (formatLine ((fst x), (snd x)) ++ formatLines xs)
Failed, modules loaded: none.
输出为print
是相同的putStrLn
这里是我的代码:
formatLine :: (String,Integer) -> String
formatLine (s, i) = s ++ "..............." ++ show i
formatLines::[(String,Integer)] -> String
formatLines [] = ""
formatLines (x:xs) = print (formatLine ((fst x), (snd x)) ++ formatLines xs)
我理解错误了print
和putStrLn
的原因,但我不知道如何解决它。
我没有完全理解。 bar的返回值是IO()并且没有输入,但是如果我希望将参数传递给bar函数?因为我需要指定输入。例如'bar s i = putStrLn(foo s i)'? – Marcio
@Marcio你可以添加额外的参数。你的例子中的'bar'将有'bar :: String - > Int - > IO()'类型。 – chi
非常感谢你!但我有更多的疑问:存在某种方式来连接条的结果与一个字符串?我试着用'show',做这样的事情:''hello“++(show baz s)',但没有奏效。出现一条消息“由于使用”show“引起的(Show(IO())的实例),我不知道可能是什么样的节目。我很抱歉,如果我利用你 – Marcio