我嘗試使用\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