Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 检查四叉树水平对称性的算法?_Algorithm_Haskell_Functional Programming_Quadtree - Fatal编程技术网

Algorithm 检查四叉树水平对称性的算法?

Algorithm 检查四叉树水平对称性的算法?,algorithm,haskell,functional-programming,quadtree,Algorithm,Haskell,Functional Programming,Quadtree,给出上面的定义,写一个谓词来检查给定的图像(编码为四叉树)是否相对于垂直轴对称(水平对称)。尽可能使用匿名函数 问题:如何对给定的四叉树执行水平对称性检查 嗯,我在想这样的事情:当四叉树只是一片叶子时,在这种情况下,我们有水平对称。基本情况是当四叉树只有一层(四片叶子)时,对称性只是检查颜色的问题(c1==c2&&c3==c4) 在任何其他情况下,我可能会检查此条件是否满足递归条件:nw等于(翻转水平(ne))和&sw等于(翻转水平(se)),其中fliphorizontal水平翻转四叉树,并检

给出上面的定义,写一个谓词来检查给定的图像(编码为四叉树)是否相对于垂直轴对称(水平对称)。尽可能使用匿名函数

问题:如何对给定的四叉树执行水平对称性检查

嗯,我在想这样的事情:当四叉树只是一片叶子时,在这种情况下,我们有水平对称。基本情况是当四叉树只有一层(四片叶子)时,对称性只是检查颜色的问题(c1==c2&&c3==c4)

在任何其他情况下,我可能会检查此条件是否满足递归条件:
nw等于(翻转水平(ne))和&sw等于(翻转水平(se))
,其中
fliphorizontal
水平翻转四叉树,并检查两个四叉树是否相等。但是,我希望尽可能避免使用外部函数,如果可能,只使用匿名函数

data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a)
    deriving (Eq, Show)
编辑:翻转示例:

ishsymmetric :: (Eq a, Show a) => QT a -> Bool
ishsymmetric (C _)                           = True
ishsymmetric (Q (C c1) (C c2) (C c3) (C c4)) = c1 == c2 && c3 == c4
ishsymmetric (Q nw ne sw se)                 =
编辑:最终单函数解决方案(使用四叉树的广义折叠函数):

比如:

ishsymmetric :: (Eq a, Show a) => QT a -> Bool
ishsymmetric (C _)       = True
ishsymmetric (Q a b c d) = and $ zipWith equals [a,c] [fliph b,fliph d]
    where
        fold f g (C c)       = g c
        fold f g (Q a b c d) = f (fold f g a) (fold f g b)
                                 (fold f g c) (fold f g d)
        fliph q = fold (\a b c d -> Q b a d c) (\c -> C c) q
        equals (C c1) (C c2)           = c1 == c2
        equals (Q a b c d) (Q e f g h) = and $ zipWith equals [a,b,c,d] [e,f,g,h]
但语法优化是可能的:-/

怎么样

ishsymmetric :: (Eq a, Show a) => QT a -> Bool
ishsymmetric (C _)                           = True
ishsymmetric (Q (C c1) (C c2) (C c3) (C c4)) = c1 == c2 && c3 == c4
ishsymmetric (Q nw ne sw se) = equals nw (fliph ne) && equals sw (fliph se)
    where equals (C a) (C b) = a == b
          equals (Q a b c d) (Q e f g h) = equals a e && equals b f && equals c g && equals d h
          fliph (C a)           = C a
          fliph (Q nw ne sw se) = Q (fliph ne) (fliph nw) (fliph se) (fliph sw)

@亚西尔·阿尔萨努卡耶夫:修复了,谢谢。编辑了第一篇文章……你可以收集并报告你的改进作为评论,然后稍后编辑这个问题,这样它就包含了评论。如果你将问题编辑8次,它将成为社区维基。CW不会生成代表。所以不要经常编辑你的帖子。我不确定,但可能
哪里的
语法符合你的需要:-@Yasir Arsanukaev:很好的起点,谢谢。或者:
等于(qa b c d)(qe f g h)=和$zipWith等于[a,b,c,d][e,f,g,h]
().@Yasir Arsanukaev:
和$
与所做的事情相同:
foldr(&&&)True(zipWith等于[a,b,c,d][e,f,g,h])
?@Gremo:yes。您可以使用GHCi中的
:t all
:t foldr(&&)True
检查函数签名。它们是相同的。此外,您可以使用
foldl1
,它是没有起始值参数的
foldl
的一个变体。顺便说一句,我已经在你的上一个问题中向所有人指出了等等,
$
是一个应用程序操作符,在
序言中定义,请参见。@Yasir Arsanukaev:我喜欢zipWith解决方案,它非常简洁优雅。我假设
不像
&&
那样短路,对吧?@Gremo:嗯,
zipWith
允许你想要的任何变形(确保结果是一个列表),而
foldl1(&&&&)
的缩写,它生成一个
Bool
值的列表。该死,为什么我把
所有的
都搞混了?:-)你是说
ishsymmetric qt=equals qt(fliph qt)
?@Gremo我很确定
equals
方法是不必要的……因为
qt
已经派生出
Eq
ishsymmetric :: (Eq a, Show a) => QT a -> Bool
ishsymmetric (C _)                           = True
ishsymmetric (Q (C c1) (C c2) (C c3) (C c4)) = c1 == c2 && c3 == c4
ishsymmetric (Q nw ne sw se) = equals nw (fliph ne) && equals sw (fliph se)
    where equals (C a) (C b) = a == b
          equals (Q a b c d) (Q e f g h) = equals a e && equals b f && equals c g && equals d h
          fliph (C a)           = C a
          fliph (Q nw ne sw se) = Q (fliph ne) (fliph nw) (fliph se) (fliph sw)
ishsymmetric qt = qt == fliph qt