Drake Demarkus
Drake Demarkus

Reputation: 27

Haskell List function errors

The function enumerate_con should return a list of the booleans inside the con constructor. for example: enumerate_con(Not(And(Con False)(Con True))) should return [False, True]

Likewise: enumerate_con(And(Con False)(Con False)) should return [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

Upvotes: 1

Views: 86

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

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. You however do not need to check the data constructor of the item wrapped in a Not. You can implement this with Not b as pattern.

Another problem is that for Con b, you return b. But the output type should be a list of Bools, not a single Bool, you thus should wrap this in a singleton list:

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]

Upvotes: 3

Related Questions