简体   繁体   中英

Unresolved top-level overloading

I need to make a Coke Vending Machine in Haskell, but I'm having some problems. I don't understand Haskell very well, so I don't know what is happening

main = start
       return()

start = do putStr "\nSelect a coin\n1. R$0,25\n2. R$0,50\n3. R$1,00\n"
           coin <- getChar;
           case coin of
                1 -> twentyFive
                2 -> fifty
                3 -> dispensed
                otherwise -> do putStr "Select a valid option"
                                start

twentyFive = do putStr "\nSelect a coin\n1. R$0,25\n2. R$0,50\n3. R$1,00\n"
                coin <- getChar;
                case coin of
                     1 -> fifty
                     2 -> seventyFive
                     3 -> dispensed
                     otherwise -> do putStr "Select a valid option"
                                     twentyFive

fifty = do putStr "\nSelect a coin\n1. R$0,25\n2. R$0,50\n3. R$1,00\n"
           coin <- getChar;
           case coin of
                1 -> seventyFive
                2 -> dispensed
                3 -> dispensed
                otherwise -> do putStr "Select a valid option"
                                fifty

seventyFive = do putStr "\nSelect a coin\n1. R$0,25\n2. R$0,50\n3. R$1,00\n"
                 coin <- getChar;
                 case coin of
                      1 -> dispensed
                      2 -> dispensed
                      3 -> dispensed
                      otherwise -> do putStr "Select a valid option"
                                      seventyFive

dispensed = do putStr "-- Coke Dispensed --"
               return()

But I am getting this error:

Unresolved top-level overloading
*** Binding             : seventyFive
*** Outstanding context : Num Char

What does this mean?

You've not indented the body of seventyFive the same way that you've indented the bodies of your other functions.

Should be indented like this instead:

seventyFive = do putStr "\nSelect a coin\n1. R$0,25\n2. R$0,50\n3. R$1,00\n"
                 coin <- getChar
                 case coin of
                      1 -> dispensed
                      2 -> dispensed
                      3 -> dispensed
                      otherwise -> do putStr "Select a valid option"
                                      seventyFive

The error message is because Hugs thinks the first line of your function is also the last line. (You had the second line not indented as far as that part of the first line that is after the do .)

Btw, use GHC instead of Hugs if you can (ideally as part of the Haskell Platform) as Hugs has not been maintained for some years.


Not an error, but it also looks odd that you have

                 coin <- getChar;

Replace with

                 coin <- getChar

More errors:

main = start
       return()

should be

main = do start
          return()

or (because we don't have to have main :: IO () , we can have main :: IO anythingWeWant ) just

main = start

And finally, you are calling getChar , which will give you a Char , but pattern matching as if it gives you a number. You need to enclose the digit in single quotes (eg '1' instead of 1 ).

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