【问题标题】:Filter function in nonogram solver非图求解器中的过滤函数
【发布时间】:2019-05-22 16:46:37
【问题描述】:

我必须了解来自https://wiki.haskell.org/Nonogram的 Ted Yin 的演绎求解器

不知道怎么弄

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]

给出False,和

all (\x -> x `elem` [1]) [1,1,1,1]

True

但我不知道要运行所有 elim 函数以及它是如何工作的

【问题讨论】:

  • 这里到底有什么不明白的地方?
  • 我无法使用任何示例参数来使其正常工作
  • 你有没有推导出elim的类型是什么?
  • 为了清楚起见,你也可以说:: (Eq a) => [a] -> [a] -> [[a]] -> [[a]]
  • “是的,我想我很清楚过滤器是如何工作的,但我不明白它是如何与 (True) 或 (False) 一起工作的”我不确定你对什么感到困惑。 filter 接受一个返回 TrueFalse 的函数,这就是它知道要过滤什么的方式。如果b 中的所有内容都在p 中,则表达式为True,并且如果w 中的任何内容都在p 中,则表达式为True。您正在过滤掉没有通过这两项测试的ps。

标签: function haskell lambda filter higher-order-functions


【解决方案1】:

首先,给自己留一点空白以帮助理解,并命名您的子表达式:

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]          

通过tst1tst2的类型来完成图片,找出ta类型之间的关系。


tst1    ::         t        -> Bool          -- tst1 x = elem    x p
tst2    ::         t        -> Bool          -- tst2 y = notElem y p
x       ::         t
y       ::         t
elem    :: Eq t => t -> [t] -> Bool
notElem :: Eq t => t -> [t] -> Bool
p       ::              [t]                  -- it was : a !

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

elim             b      w               ps  :: [[t]]
elim :: Eq t => [t] -> [t] ->         [[t]] -> [[t]] 

那么filter foo 只在ps 中留下ps 中的foo p == True

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

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

foo p =  (b \\ p) == []   &&   (p \\ w) == p

持有:

import Data.List (\\)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2020-09-19
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多