简体   繁体   English

Haskell 列出 function 错误

[英]Haskell List function errors

The function enumerate_con should return a list of the booleans inside the con constructor. function enumerate_con 应该返回 con 构造函数中的布尔值列表。 for example: enumerate_con(Not(And(Con False)(Con True))) should return [False, True]例如: enumerate_con(Not(And(Con False)(Con True)))应该返回[False, True]

Likewise: enumerate_con(And(Con False)(Con False)) should return [False, False].同样: enumerate_con(And(Con False)(Con False))应该返回 [False, False]。

Here is the code I have:这是我的代码:

 enumerate_con :: Formula -> [Bool]
 enumerate_con (And q1 q2) = enumerate_con q2 ++ enumerate_con q2
 enumerate_con (Not (Con True)) = [True]
 enumerate_con (Not (Con False)) = [False]
 enumerate_con (Con b) = b

Here is how formula is defined:以下是公式的定义方式:

 data Formula = And Formula Formula
              | Not Formula
              | Con Bool
              deriving Show

Please help me fix the function so it works as intended请帮助我修复 function,使其按预期工作

The problem is that Not (And (Con True) (Con True)) can occur, but your pattterns will only work if the Not has as parameter a Con True or a Con False .问题是Not (And (Con True) (Con True))可能会发生,但只有当Not的参数为Con TrueCon False时,您的模式才会起作用。 You however do not need to check the data constructor of the item wrapped in a Not .但是,您不需要检查包裹在Not中的项目的数据构造函数。 You can implement this with Not b as pattern.您可以使用Not b作为模式来实现它。

Another problem is that for Con b , you return b .另一个问题是,对于Con b ,您返回b But the output type should be a list of Bool s, not a single Bool , you thus should wrap this in a singleton list:但是 output 类型应该是Bool列表,而不是单个Bool ,因此您应该将其包装在 singleton 列表中:

enumerate_con :: Formula -> [Bool]
enumerate_con (And q1 q2) = enumerate_con q2 ++ enumerate_con q2
enumerate_con (Not b) = enumerate_con b
enumerate_con (Con b) = [b]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM