2

所以我试图创建这个函数 AgregarMon,它基本上向“Polinomio”添加了一个“Monomio”,Monomio 最终会成为 Polinomio 中的一个元素,它是一个列表。你会更好地理解一点点

type Monomio = (Int, Int)
type Polinomio = [Monomio]

agregarMon :: Monomio -> Polinomio -> Polinomio
agregarMon = \m p -> case m of{ (0,0) -> p;
                                 x -> case p of{[] -> [x];
                                                y:ys -> case (snd x == snd y) of { true -> case ((fst x + fst y)==0) of { true -> ys;
                                                                                                                          false -> (fst x + fst y , snd x):ys;}
                                                                                   false -> case snd x < snd y of{true -> y: agregarMon x ys;
                                                                                                                  false -> x:y:ys;}}}}

我一直在看我的代码一个小时,但我找不到问题。错误说:

Polinomios.hs:45:140: error:
    Unexpected case expression in function application:
        case ((fst x + fst y) == 0) of
          true -> ys
          false -> (fst x + fst y, snd x) : ys
    You could write it with parentheses
    Or perhaps you meant to enable BlockArguments?
   |
45 |                                                                                                 y:ys -> case (snd x == snd y) of { true -> case ((fst x + fst y)==0) of { true -> ys;    |                                                                                                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

第 45 行是上面代码的第四行。对不起,如果我缺少信息,请让我现在。以防万一,有人不知道,fst 和 snd 在 Prelude 上。fst 从“Monomio”中获取第一个元素,然后获取第二个元素。fst = (a,b)->a snd (a,b) -> b

4

3 回答 3

2

虽然您可以在紧要关头使用大括号和分号,但 Haskell 不是基于 C 的语言。它习惯性地使用缩进来指示函数范围。

这样的事情会更惯用:

agregarMon :: Monomio -> Polinomio -> Polinomio
agregarMon = \m p -> case m of
                      (0,0) -> p
                      x -> case p of
                            [] -> [x]
                            y:ys -> case (snd x == snd y) of
                                      true -> case ((fst x + fst y)==0) of
                                                true -> ys
                                                false -> (fst x + fst y , snd x):ys
                                      false -> case snd x < snd y of
                                                true -> y: agregarMon x ys
                                                false -> x:y:ys

这可以编译(尽管有警告,见下文),但我不知道它是否符合您的要求。OP 代码不编译,所以很明显,编译的东西是等价的。

然而,我所做的只是删除所有大括号和分号,而是“修复”缩进。

在我看来,代码仍然缩进太多,但至少(有一个例外)它保持在 80 个字符的左边。宽代码可能会迫使人们水平滚动,这不会让你交到很多朋友。

上面的代码在 GHCi 中加载,但带有警告:

[1 of 1] Compiling Q58986486        ( 58986486.hs, interpreted )

58986486.hs:14:49: warning: [-Woverlapping-patterns]
    Pattern match is redundant
    In a case alternative: false -> ...
   |
14 |                                                 false -> (fst x + fst y , snd x):ys
   |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

58986486.hs:15:39: warning: [-Woverlapping-patterns]
    Pattern match is redundant
    In a case alternative: false -> ...
   |
15 |                                       false -> case snd x < snd y of
   |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

58986486.hs:17:49: warning: [-Woverlapping-patterns]
    Pattern match is redundant
    In a case alternative: false -> ...
   |
17 |                                                 false -> x:y:ys
   |                                                 ^^^^^^^^^^^^^^^
Ok, one module loaded.

因此,我不确定它是否按预期工作。

于 2019-11-22T07:35:58.310 回答
2

Mark Seemann 的版本虽然可以改进。

首先,让我们提出mp实际的论点agregarMon

agregarMon :: Monomio -> Polinomio -> Polinomio
agregarMon m p = case m of
                      (0,0) -> p
                      x -> case p of
                            [] -> [x]
                            y:ys -> case (snd x == snd y) of
                                      true -> case ((fst x + fst y)==0) of
                                                true -> ys
                                                false -> (fst x + fst y , snd x):ys
                                      false -> case snd x < snd y of
                                                true -> y: agregarMon x ys
                                                false -> x:y:ys

现在我们可以在 agruments 上使用模式匹配来简化代码:

agregarMon :: Monomio -> Polinomio -> Polinomio
agregarMon (0, 0) p = p
agregarMon x [] = [x]
agregarMon (f, s) (y:ys) = case (snd x == snd y) of
                            true -> case ((fst x + fst y)==0) of
                                         true -> ys
                                         false -> (fst x + fst y , snd x):ys
                            false -> case snd x < snd y of
                                          true -> y: agregarMon x ys
                                          false -> x:y:ys

case通过条件检查、更好的使用if或保护的结果进行模式匹配并不是那么惯用的:

agregarMon :: Monomio -> Polinomio -> Polinomio
agregarMon (0, 0) p = p
agregarMon x [] = [x]
agregarMon x@(coefx, powx) (y@(coefy, powy):ys)
  | powx == powy = if coefx + coefy == 0
                   then ys
                   else (coefx + coefy, powx):ys
  | powx < powy = y:agregarMon x ys
  | otherwise = x:y:ys

现在我们有一些简单的案例和一个复杂的案例,但即使是复杂的案例也不难阅读和理解(我什至假设您正在使用多项式、系数和幂)。

你可以在那里运行它:https ://repl.it/@Yuri12358/so-monomio-polinomio

也许此链接将帮助您提高对在代码中进行分支的不同方法的理解:http: //learnyouahaskell.com/syntax-in-functions#pattern-matching

于 2019-11-22T10:48:45.130 回答
2
case (snd x == snd y) of { true ->

错了:true这里只是一个变量名,与构造函数无关True(大写T!)。因此,上面的代码片段等价于

case (snd x == snd y) of { x ->

因此,该模式匹配任何布尔值,True并且False. 因此,false -> ...永远不会考虑另一个分支。

我建议您打开警告,因为这样做会使 GHC 将第二个分支报告为冗余代码。编译器认为分支无用的事实表明代码存在严重问题。

最后一点,在我的经验代码中

case something of
  True  -> a
  False -> b

并不常见,因为我们可以把它写成

if something
then a
else b

在我看来,这更容易阅读。

于 2019-11-22T19:34:23.423 回答