這是我的代碼
main :: IO ()
main = do
putStrLn "Pick a number?"
putStrLn "From 1-5"
numPick <- getLine
putStrLn "Thank you for choosing a number."
if numPick == 1 then
do createProcess (proc "/usr/bin/ls" [])
else
do putStrLn "Do nothing"
putStrLn "Were done here"
我希望用戶選擇一個數字,然后從選擇的數字中運行一個系統行程。我是 Haskell 的新手,有人知道我做錯了什么嗎?
嘗試編譯時出現以下錯誤。
hlsurl.hs:18:11: error:
* Couldn't match type `()'
with `(Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)'
Expected type: IO
(Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
Actual type: IO ()
* In a stmt of a 'do' block: putStrLn "Do nothing"
In the expression: do putStrLn "Do nothing"
In a stmt of a 'do' block:
if numPick == 1 then
do createProcess (proc "/usr/bin/ls" [])
else
do putStrLn "Do nothing"
|
18 | do putStrLn "Do nothing"
| ^^^^^^^^^^^^^^^^^^^^^
uj5u.com熱心網友回復:
createProcess回傳與行程關聯的多個句柄,而不putStrLn回傳任何內容 ( IO ())。因為 - 運算式的兩個部分都if-else需要屬于同一型別,所以您需要統一這些函式呼叫,例如通過使用“吞下”createProcess的回傳值void:
main :: IO ()
main = do
putStrLn "Pick a number?"
putStrLn "From 1-5"
numPick <- getLine
putStrLn "Thank you for choosing a number."
if numPick == "1" then -- also fixed "1", because `getLine` returns String
void $ createProcess (proc "/usr/bin/ls" [])
else
putStrLn "Do nothing"
putStrLn "Were done here"
沒有的等效代碼void是:
if numPick == "1" then do
createProcess (proc "/usr/bin/ls" [])
return ()
else
putStrLn "Do nothing"
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412888.html
標籤:
上一篇:白話TCP/IP原理
