我认为括号有问题

问题描述 投票:-3回答:2

对不起,我整天都发布了代码,现在我有了这个代码,我也不知道为什么它不起作用。我有一个错误并不能告诉我确切的问题是什么,有人告诉我打开警告,这样可能会更容易,但我不知道该怎么做。如果将此代码赋予单项式,则应将其作为字符串。单调式是(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";     | 
string haskell switch-statement show
2个回答
2
投票

据我所知,您可以将整个功能简化为一些简单的情况。

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


0
投票

这里是您的代码,为了清楚起见,已重新缩进。仍然很难阅读。

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一样加入支票。

© www.soinside.com 2019 - 2024. All rights reserved.