我是Haskell的新手,想知道如何在Haskell中打印两个函数的结果。在C++中,我会这样做:
cout << f() << g();
或者在 c# 中:
Console.WriteLine(f() + " " + g());
在Haskell中,我尝试了类似于以下的操作:
main =
--putStr ( show $ square 3 )
putStr ( show $ fibSeries 12 )
square :: Int -> Int
square x = x * x
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
fibSeries :: Int -> [Int]
fibSeries x = map fib [0..x]
但是我必须注释掉第一个命令,因为它给我带来了编译时错误。
错误信息如下:
src\Main.hs:21:5:
Couldn't match expected type `(String -> IO ()) -> String -> t0'
with actual type `IO ()'
The function `putStr' is applied to three arguments,
but its type `String -> IO ()' has only one
In the expression:
putStr (show $ square 3) putStr (show $ fibSeries 12)
In an equation for `main':
main = putStr (show $ square 3) putStr (show $ fibSeries 12)