在此代碼中存在錯誤起因以致不匹配。由於類型不匹配導致的代碼中的錯誤
import System.IO
loop :: Int -> [Int] -> IO()
loop 0 ls = return ls
loop n ls = do newNumber <- readLn
loop (n-1) (newNumber:ls)
main = do
putStrLn "Please enter the number"
number <- readLn :: IO Int
putStrLn $ "The num is:" ++ show number
xs <- loop number []
print xs
的錯誤信息是:
treep.hs:4:20:
Couldn't match expected type `()' with actual type `[Int]'
In the first argument of `return', namely `ls'
In the expression: return ls
In an equation for `loop': loop 0 ls = return ls
如何刪除錯誤
@tikhon請看這段代碼 – 2012-01-16 08:55:55
你想用'循環'功能完成什麼?它的簽名表示它需要一個整數('Int')整數列表('[Int]')並返回一個操作。 – Andre 2012-01-16 09:10:48
我想讓循環運行n次,並最終返回n個數字列表 – 2012-01-16 09:13:00