[英]Smart looping over lists using | in Haskell
I am trying to build a logic sentence (such that only m propositions out of n propositions can be true) with a double loop, but get confused by the "|"我正在尝试用双循环构建一个逻辑句子(这样 n 个命题中只有 m 个命题可以为真),但被“|”弄糊涂了token.
令牌。 I cannot find its precise meaning on Hoogle.
我在 Hoogle 上找不到它的确切含义。 The select gives a list of lists, a list of indexes that can be selected.
select 给出了一个列表列表,一个可以选择的索引列表。 With the indexes i want to build a conjunction of positive "selected" propositions and negative "non-selected" propositions.
通过索引,我想建立积极的“选定”命题和消极的“非选定”命题的结合。 What am i doing wrong with the following code?
下面的代码我做错了什么?
genXorM :: Int -> Int -> Form
genXorM n m = Disj [Conj [Neg $ PrpF $ P x, PrpF $ P y] | z <- select, y <- [0 .. n] \\ z, x <- z] where
select = combinations m [0 .. n]
genXorM :: Int -> Int -> Form
genXorM n m = Disj [Conj [Neg $ PrpF $ P x, PrpF $ P y] | z <- select, y <- [0 .. n] \\ z, x <- z] where
select = combinations m [0 .. n]
You have a “triple loop” here.你在这里有一个“三重循环”。 It says:
它说:
If I understand correctly that you want a conjunction of all the selected propositions x with the negation of all the non-selected ones y , that could be written with list comprehensions like this:如果我理解正确,您希望将所有选定的命题x与所有未选定的命题y结合起来,那么可以用这样的列表推导式编写:
genXorM :: Int -> Int -> Form
genXorM n m = Disj
[ Conj
([PrpF (P p) | p <- x]
++ [Neg (PrpF (P n)) | n <- y])
| x <- select
, let y = [0 .. n] \\ x
]
where
select = combinations m [0 .. n]
Or without them, for example, using map
and some helper functions to break down the problem into smaller pieces:或者没有它们,例如,使用
map
和一些辅助函数将问题分解为更小的部分:
genXorM n m = Disj (map conjoin selected)
where
selected = combinations m [0 .. n]
conjoin z = Conj (map positive z ++ map negative (complement z))
positive = PrpF . P
negative = Neg . positive
complement x = [0 .. n] \\ x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.