[英]Haskell - Main function for calling all other functions using user input
我是Haskell的新手,自從Java以來,就發現這一切令人困惑。
我正在嘗試實現一個功能,該功能在命令行中提示用戶輸入數字,然后執行與該輸入相關的功能,如果沒有輸入任何輸入,則默認為第一個功能。
我在使用if函數時遇到問題,編譯器不斷給我錯誤,我不確定為什么。
main = do
putStr "Enter question number: "
xs <- getLine
if (xs == "3")
then
putStr "number of toppings: "
top <- getLine
putStr "diameter of pizza in cm2: "
size <- getLine
(pizzaPrice (top size))
else
putStr "Enter 3 numbers: "
args <- getLine
(numAbove1 (args))
我收到編譯器錯誤“輸入'if'解析錯誤”。
有人可以告訴我我在做什么錯。
您需要在then和else子句中添加一個do
:
if ...
then do
putStr ...
...
else do
putStr ...
...
另外,請確保if
語句的縮進量與xs <- ...
語句的縮進量相同,並且不要使用制表符。
例如:
main = do
putStr "Enter question number: "
xs <- getLine
if (xs == "3")
then do
putStr "number of toppings: "
top <- getLine
putStr "diameter of pizza in cm2: "
size <- getLine
return ()
else do
putStr "Enter 3 numbers: "
args <- getLine
return()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.