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
seventyFivethe same way that you’ve indented the bodies of your other functions.Should be indented like this instead:
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
Replace with
More errors:
should be
or (because we don’t have to have
main :: IO (), we can havemain :: IO anythingWeWant) justAnd finally, you are calling
getChar, which will give you aChar, but pattern matching as if it gives you a number. You need to enclose the digit in single quotes (e.g.'1'instead of1).