在Haskell中,我試圖打印一個返回Int
的方法。目前,mySum
只是一個存根,因爲我試圖找出如何打印它。返回int的打印函數
我擡起頭來如何做到這一點,我看到putStr
可以打印String
並顯示轉換的Int
爲String
,所以我這樣做:
mySum :: [Int] -> Int
mySum _ = 0
main = putStr show mySum [1..5]
不過,我收到這些錯誤:
Couldn't match expected type ‘([Int] -> Int) -> [Integer] -> t’
with actual type ‘IO()’
Relevant bindings include main :: t (bound at weirdFold.hs:10:1)
The function ‘putStr’ is applied to three arguments,
but its type ‘String -> IO()’ has only one
In the expression: putStr show mySum [1 .. 5]
In an equation for ‘main’: main = putStr show mySum [1 .. 5]
和
Couldn't match type ‘a0 -> String’ with ‘[Char]’
Expected type: String
Actual type: a0 -> String
Probable cause: ‘show’ is applied to too few arguments
In the first argument of ‘putStr’, namely ‘show’
In the expression: putStr show mySum [1 .. 5]
那麼我該如何打印方法的結果呢?
嘗試加入一些括號:'主要= putStr(顯示(mySum [1..5]))'。功能應用程序是關聯的。 – user2297560
標題的第一印象:您正試圖打印一個函數(而不是您在應用函數時獲得的值)。你可以很容易地得出解決方案,意識到你只是想打印一個'Int'而不是一個函數。像'print n'('print = putStrLn。show'),然後替換'n':'print(mySum [1..5])'。爲簡單起見,我使用了'print',但您可以輕鬆使用'putStr。展示「或其他任何東西。 – jakubdaniel