为什么Haskell抱怨这个加号?

时间:2019-03-08 14:06:39

标签: haskell

此代码旨在将平衡的三元表示形式转换为Haskell Integer

frombal3 :: String -> Integer
frombal3 "+" =  1
frombal3 "0" =  0
frombal3 "-" = -1
frombal3 current:therest = \
  (*) frombal3 current (^) 3 length therest \
  + frombal3 therest

我得到了错误:

main.hs:7:3: error: parse error on input ‘+’
  |
7 |   + frombal3 therest
  |   ^
<interactive>:3:1: error:
   • Variable not in scope: main
   • Perhaps you meant ‘min’ (imported from Prelude)

1 个答案:

答案 0 :(得分:1)

请勿使用反斜杠,并记住正确将模式匹配括在括号中:

frombal3 :: String -> Integer
frombal3 "+" =  1
frombal3 "0" =  0
frombal3 "-" = -1
frombal3 (current:therest) = -- ^ Note brackets
  (*) frombal3 current (^) 3 length therest 
  + frombal3 therest

由于您如何使用运算符,这仍然会引起问题,但是我认为您可以自己解决此问题,尤其是因为我无法弄清您要在这里做什么。