我是新來的haskell球員。我正在嘗試編寫一個gcd可執行文件。無法與實際類型`[a0]'匹配的預期類型`IO b0'
ghc --make gcd
當我編譯此代碼時出現以下錯誤。
Couldn't match expected type `IO b0' with actual type `[a0]'
In a stmt of a 'do' block:
putStrLn "GCD is: " ++ gcd' num1 num2 ++ "TADA...."
In the expression:
do { putStrLn "Hello,World. This is coming from Haskell";
putStrLn "This is the GCD";
putStrLn "Frist Number";
input <- getLine;
.... }
In an equation for `main':
main
= do { putStrLn "Hello,World. This is coming from Haskell";
putStrLn "This is the GCD";
putStrLn "Frist Number";
.... }
我不明白我的問題在哪裏...這是我的代碼。
gcd' :: (Integral a) => a -> a -> a
gcd' x y = gcd' (abs x) (abs y)
where gcd' a 0 = a
gcd' a b = gcd' b (a `rem` b)
main = do
putStrLn "Hello,World. This is coming from Haskell"
putStrLn "This is the GCD"
putStrLn "Frist Number"
input <- getLine
let num1 = (read input)
putStrLn "Second Number"
input2 <- getLine
let num2 = read input2
putStrLn "GCD is: " ++ gcd' num1 num2 ++ "TADA...."
我所知道的是,read
幫助我將我的字符串轉換爲int。
你是一個天才!謝謝!! – user805981
使我的程序看起來更好。有沒有一種方法可以使輸入區域靠近聲明而不是一條線? – user805981
當然(通常),我會將它添加到答案中,因爲有一點可能不適合評論的緩衝。 –