为Quadtrees写入太多模式匹配? [英] Too many pattern matches to write down for Quadtrees?

查看:146
本文介绍了为Quadtrees写入太多模式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个四叉树定义如下:

  data(Eq a,Show a)= > QT a = C a | Q(QT a)(QT a)(QT a)(QT a)
导出(Eq,Show)

bad1 = Q uuuu其中u = C 255
bad2 = Q(C 0)(C 255)(Q uuuu)(C 64)其中u = C 255

该构造函数允许您创建格式不正确的四叉树。 bad1 应该是简单的C 255和 bad2 也是无效的,因为它的右下四叉树(出于同样的原因,它应该是 Q(C 0)(C 255)(C 244)(C 64)



检查它的良好形状只是递归检查其内部四叉树的问题。基本情况是当所有内部四叉树都是叶子时,所有颜色都不应该是'

  wellformed ::(等式a,显示a)=> QT a  - > Bool $ b (C c1)(C c2)(C c3)(C c4))=任何(c = c1)[c2,c3,c4] 
)se(C c4))=有效se
- 继续定义模式以匹配例如QCCC,CQQC等...

问题我可以避免输入所有可能的叶子和四叉树组合吗?

如果我的问题很奇怪,请耐心等待,但这是我的第二天Haskell-seaml ess-learing!

解决方案



  wellformed ::(等式a,显示a)=> QT a  - > Bool 
wellformed(C_)= True
wellformed(Q(C c1)(C c2)(C c3)(C c4))= any(/ = c1)[c2,c3,c4]
wellformed(Q nw ne se sw)= wellformed nw&&良好的ne
&&良好的se&& wellform sw

编辑:甚至更好:

  wellformed ::(等式a,显示a)=> QT a  - > Bool 
wellformed(C_)= True
wellformed(Q(C c1)(C c2)(C c3)(C c4))= any(/ = c1)[c2,c3,c4]
wellformed(Q nw ne se sw)= all wellformed [nw,ne,se,sw]

编辑:请注意绑定错误,应该是:NW NE SW SE !!!


Imagine a quadtree defined as follow:

data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a)
  deriving (Eq, Show)

bad1 = Q u u u u where u = C 255
bad2 = Q (C 0) (C 255) (Q u u u u) (C 64) where u = C 255

The constructor allows you to create not well-formed quadtrees. bad1 should be simply C 255 and bad2 is not valid too because its bottom-right quadtree (for the same reason, it should be Q (C 0) (C 255) (C 244) (C 64).

So far so good. Checking its well-formness is simply a matter of checking its inner quadtrees recursively. The base case is when all inner quadtrees are leafs, whereby all colors shouldn't be all equals.

wellformed :: (Eq a, Show a) => QT a -> Bool
wellformed (Q (C c1) (C c2) (C c3) (C c4)) = any (/= c1) [c2, c3, c4]
wellformed (Q (C c1) (C c2) se (C c4))     = valid se
-- continue defining patters to match e.g Q C C C, C Q Q C, and so on...

Question: Can I avoid typing all matches for all possible combination of leafs and quadtrees?

Please be patient if my question is quite odd, but it's my second-day-Haskell-seamless-learing!

解决方案

Nevermind...

wellformed :: (Eq a, Show a) => QT a -> Bool
wellformed (C _) = True
wellformed (Q (C c1) (C c2) (C c3) (C c4)) = any (/= c1) [c2, c3, c4]
wellformed (Q nw ne se sw) = wellformed nw && wellformed ne
   && wellformed se && wellformed sw

EDIT: or even better:

wellformed :: (Eq a, Show a) => QT a -> Bool
wellformed (C _) = True
wellformed (Q (C c1) (C c2) (C c3) (C c4)) = any (/= c1) [c2, c3, c4]
wellformed (Q nw ne se sw) = all wellformed [nw, ne, se, sw]

EDIT: note that the bindings are wrong, should be: NW NE SW SE!!!

这篇关于为Quadtrees写入太多模式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆