未解决的顶级重载

时间:2012-06-25 18:36:37

标签: haskell

我需要在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

这是什么意思?

1 个答案:

答案 0 :(得分: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之后第一行的那一部分。)

顺便说一下,如果可以的话,可以使用GHC代替Hugs(理想情况下是Haskell平台的一部分),因为Hugs已经维护了好几年了。


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

                 coin <- getChar;

替换为

                 coin <- getChar

更多错误:

main = start
       return()

应该是

main = do start
          return()

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

main = start

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