简体   繁体   中英

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.

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.

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'".

Can someone please explain to me what I am doing wrong.

You need to add a do to the then and else clauses:

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM