我想我的括号有问题

I think i have a problem with the parenthesces

对不起,我一整天都在发布代码,现在我有了这个,但我不知道为什么它不起作用。我有一个错误,它没有告诉我我的问题到底是什么,有人告诉我打开警告,这样它可能会更容易,但我不知道该怎么做。如果你给这段代码一个单项式,它应该给你一个字符串形式的单项式。 monomian 是 a (a,b),所以这就是代码中充满 fst 和 snd 的原因。

showMon :: Monomio -> String
showMon = \m -> case (fst m) == 0 of{ True -> " ";
                                    False -> case (fst m) == 1 of{ True -> case (snd m) == 0 of{ True -> "1";
                                                                                               False -> case (snd m) == 1 of{ True-> "x";
                                                                                                                            False -> "x^" ++ (show (snd m));}}
                                                                   False -> case (fst m) < 0 of{ True -> case (snd m) == 0 of{ True -> show 1;
                                                                                                                     False -> case (snd m) == 1 of { True -> "-x";
                                                                                                                                                     False -> "-x^" ++ show (snd m);}}                                                                                        
                                                                                          False -> case snd m == 0 of{ True -> show 1;
                                                                                                                       False -> case snd m == 1 of{ True-> (show fst m) ++ "x";
                                                                                                                                                    False-> (show fst m) ++ "x^" ++ show (snd m);}}}}}
Polinomios.hs:146:108: error:
    Unexpected case expression in function application:
        case (snd m) == 0 of
          True -> "1"
          False
            -> case (snd m) == 1 of
                 True -> "x"
                 False -> "x^" ++ (show (snd m))
    You could write it with parentheses
    Or perhaps you meant to enable BlockArguments?
    |
146 |                                                                     False -> case (fst m) == 1 of{ True -> case (snd m) == 0 of{ True -> "1";     | 

据我所知,您可以将整个功能简化为几个简单的案例。

showMon (m, n) | m < 0 = "-" ++ showMon (negate m, n)  -- negative terms
showMon (0, _) = " "  -- empty term
showMon (m, 0) = show m -- terms with x^0
showMon (m, n) = let coeff = if m == 1 then "" else (show m)
                     exp = if n == 1 then "" else ("^" ++ show n)
                 in coeff ++ "x" ++ exp -- other terms, with coefficients and exponents of 1 dropped

一个case表达式可以处理多个不同的值。如果您 do 需要匹配布尔值,这就是 if 表达式的用途。

if x then y else z

等同于

case x of
  True -> y
  otherwise -> z

这是您的代码,为清楚起见重新缩进了代码。还是挺难读的。

showMon :: Monomio -> String
showMon = \m -> case (fst m) == 0 of
  { True -> " ";
    False -> case (fst m) == 1 of
      { True -> case (snd m) == 0 of
          { True -> "1";
            False -> case (snd m) == 1 of
              { True-> "x";
                False -> "x^" ++ (show (snd m));
              }
          } -- **
        False -> case (fst m) < 0 of
          { True -> case (snd m) == 0 of
              { True -> show 1;
                False -> case (snd m) == 1 of
                  { True -> "-x";
                    False -> "-x^" ++ show (snd m);
                  }
              } -- **
            False -> case snd m == 0 of
              { True -> show 1;
                False -> case snd m == 1 of
                  { True-> (show fst m) ++ "x";
                    False-> (show fst m) ++ "x^" ++ show (snd m);
                  }
              }
          }
      }
  }

在上面标有 -- ** 的点中,缺少分号,使编译器产生奇怪的错误消息,因为我们实际上是在尝试以这种方式将 case 表达式应用于某些参数.

一些改进建议:

您不必使用 fst,snd,您可以对输入参数进行模式匹配。而不是\m -> ...,你可以使用\(m1,m2) -> ...,然后直接使用这两个组件。

您不必以那种方式检查 fst m == 0fst m == 1。您可以改用(尝试模仿您的风格)

case m1 of  -- recall m1 is (fst m)
  { 0 -> .... ;  -- it is 0
    1 -> .... ;  -- it is 1
    _ -> ....    -- it is something else
  }

原则上,有了守卫,你甚至可以像你的 fst m < 0.