gpt4 book ai didi

function - 我在使用 Haskell 时遇到错误,我无法找到这些案例

转载 作者:行者123 更新时间:2023-12-02 00:11:13 24 4
gpt4 key购买 nike

所以我正在尝试创建这个 AgregarMon 函数,基本上是将“Monomio”添加到“Polinomio”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

最佳答案

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

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

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

因此,该模式匹配任何 bool 值,TrueFalse。因此,永远不会考虑另一个分支 false -> ...

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

作为最后的说明,根据我的经验,代码如下

case something of
True -> a
False -> b

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

if something
then a
else b

在我看来,哪个更容易阅读。

关于function - 我在使用 Haskell 时遇到错误,我无法找到这些案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58986486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com