0

我需要在 Haskell 中制作可乐自动售货机,但我遇到了一些问题。我不太了解Haskell,所以我不知道发生了什么

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

但我收到此错误:

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

这是什么意思?

4

1 回答 1

2

您缩进的seventyFive方式与缩进其他函数的主体的方式不同。

应该像这样缩进:

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

错误信息是因为 Hugs 认为你的函数的第一行也是最后一行。(第二行没有缩进到第一行之后的那部分do。)

顺便说一句,如果可以(最好作为 Haskell 平台的一部分)使用 GHC 而不是 Hugs,因为 Hugs 已经有几年没有维护了。


不是错误,但看起来也很奇怪

                 coin <- getChar;

用。。。来代替

                 coin <- getChar

更多错误:

main = start
       return()

应该

main = do start
          return()

或(因为我们不必拥有main :: IO (),我们可以拥有main :: IO anythingWeWant)只是

main = start

最后,你正在调用getChar,它会给你一个Char,但是模式匹配就好像它给你一个数字一样。您需要将数字括在单引号中(例如'1',而不是1)。

于 2012-06-25T18:40:10.760 回答