Function 非图解算器中的滤波函数

Function 非图解算器中的滤波函数,function,haskell,lambda,filter,higher-order-functions,Function,Haskell,Lambda,Filter,Higher Order Functions,我必须从一开始就理解尹泰德的演绎解算器 我不知道怎么做 elim b w ps = filter (\p -> all (\x -> x `elem` p) b && all (\x -> x `notElem` p) w) ps 工作。我只知道 all (\x -> x `notElem` [1]) [1,2,3,4] 给出假,以及 all (\x -> x `elem` [1]) [1,

我必须从一开始就理解尹泰德的演绎解算器

我不知道怎么做

elim b w ps = filter (\p -> all (\x -> x `elem` p) b &&
                            all (\x -> x `notElem` p) w) ps
工作。我只知道

all (\x -> x `notElem` [1]) [1,2,3,4]
给出
,以及

all (\x -> x `elem` [1]) [1,1,1,1]
给出
真值


但我不知道如何运行all
elim
函数,以及它是如何工作的。首先,请使用一些空格来帮助理解,并命名子表达式:

elim b w ps = filter (\p -> all (\x -> x `elem`    p) b  &&
                            all (\x -> x `notElem` p) w
                       ) ps

            = filter foo ps
                where
                   foo p =  all (\x -> x `elem`    p) b  &&
                            all (\x -> x `notElem` p) w

            = filter foo ps
                where
                   foo p =  all tst1 b  &&  all tst2 w
                      where
                         tst1 = (\x -> x `elem`    p)
                         tst2 = (\x -> x `notElem` p)

            = filter foo ps
                where
                   foo p =  (&&)  (all tst1 b)  (all tst2 w)
                      where
                         tst1 x = elem    x p
                         tst2 y = notElem y p
那又有什么用呢?或者更好的是,它是什么?让我们通过一些类型来建立我们的洞察力:

filter :: (a ->                Bool) -> [a] -> [a]
foo    ::  a ->                Bool
ps     ::                               [a]
filter    foo                           ps  :: [a]
p      ::  a
foo        p                :: Bool
(&&)        :: Bool -> Bool -> Bool
all tst1 b  :: Bool
all tst2 w  ::         Bool
---------------------------
all  :: (t -> Bool) -> [t] -> Bool
tst1 ::  t -> Bool
tst2 ::  t -> Bool
b    ::                [t]
w    ::                [t]
---------------------------
......
---------------------------
elim             b      w               ps  :: [a]
elim ::         [t] -> [t] ->           [a] -> [a]          
通过使用
tst1
tst2
的类型来完成图片,以找出
t
a
类型之间的关系


因此,
a~[t]
[a]~[[t]]
最后

elim             b      w               ps  :: [[t]]
elim :: Eq t => [t] -> [t] ->         [[t]] -> [[t]] 
因此,
filter foo
只在
ps
中保留那些
foo p==True的
p
s

这意味着
all tst1 b==True
all tst2 w==True

这意味着,
b
中的每个
x
都是
p
的元素,而
w
中的每个
y
都不是
p
中的元素。或者换句话说,只有
ps
中的
p
s在结果列表中被单独保留

foo p =  (b \\ p) == []   &&   (p \\ w) == p
持有:

import Data.List (\\)

elim b w ps = [ p | p <- ps, (b \\ p) == [], (p \\ w) == p ]
导入数据。列表(\\)

elim b w ps=[p | p你到底不明白什么?我不能用任何示例参数来说明它的工作原理。你推导出了elim
的类型吗?为了清楚起见,你也可以说::(Eq a)=>[a]->[a]->[a]->[[a]]
“是的,我想我很清楚过滤器是如何工作的,但我不明白它是如何与(True)或(虚假)我不知道你到底在想什么。
filter
接受一个返回
True
False
的函数,这就是它知道要过滤什么的原因。如果
b
中的所有内容都在
p
中,则表达式是
True
如果
w
p
中。您正在筛选出
p
中未通过这两项测试的。
import Data.List (\\)

elim b w ps = [ p | p <- ps, (b \\ p) == [], (p \\ w) == p ]