[英]Haskell - Main function for calling all other functions using user input
I'm new to Haskell and finding it all a bit confusing since coming from Java. 我是Haskell的新手,自从Java以来,就发现这一切令人困惑。
I am trying to implement a function that prompts the user in command line for a number input and then execute the function correlating to the input, or default to the first function if no input is entered. 我正在尝试实现一个功能,该功能在命令行中提示用户输入数字,然后执行与该输入相关的功能,如果没有输入任何输入,则默认为第一个功能。
I'm having a problem with the if function, the compiler keeps giving me errors and I'm not sure why. 我在使用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))
I'm getting the compiler error "parse error on input 'if'". 我收到编译器错误“输入'if'解析错误”。
Can someone please explain to me what I am doing wrong. 有人可以告诉我我在做什么错。
You need to add a do
to the then and else clauses: 您需要在then和else子句中添加一个do
:
if ...
then do
putStr ...
...
else do
putStr ...
...
Also, make sure the if
statement is indented the same amount as the xs <- ...
statement -- and don't use tabs. 另外,请确保if
语句的缩进量与xs <- ...
语句的缩进量相同,并且不要使用制表符。
Eg: 例如:
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.