简体   繁体   English

如何在Haskell中压缩玫瑰树

[英]How to zip Rose Trees in Haskell

For a Haskell Tic Tac Toe game, I am trying to zip two Rose Trees. 对于Haskell Tic Tac Toe游戏,我尝试拉紧两棵玫瑰树。 I have defined the rose tree as 我把玫瑰树定义为

data Rose a = a :> [Rose a]

I have tried the following: 我尝试了以下方法:

zipTrees :: Rose Board -> Rose Int -> Rose (Board, Int)
zipTrees (b :> []) (i :> []) = (b,i) :> []
zipTrees (b :> chib) (i :> chii) = (b, i) :> zipTrees chib chii

but an error occurs, because it couldn't match expected type Rose Int and Rose Board with [Rose Int] and [Rose Board] respectively. 但是发生错误,因为它无法分别将预期的Rose IntRose Board[Rose Int][Rose Board]匹配。 I am clueless about how to zip the trees in any other way. 我不知道如何以其他任何方式拉紧树木。

To illustrate further, if I wanted to zip the trees 为了进一步说明,如果我想拉紧树木

a :> [a :> [a :> []], a :> []]

and b :> [b :> [b :> []], b :> []] , I want to return the tree b :> [b :> [b :> []], b :> []] ,我想返回树

(a, b) :> [(a, b) :> [(a, b) :> []], (a,b) :> []]

chib and chii are lists of trees, not trees themselves. chibchii是树木的清单,而不是树木本身。 You can't just call zipTrees on them. 您不能只对它们调用zipTrees You will need to either recurse 您将需要递归

zipTrees :: Rose Board -> Rose Int -> Rose (Board, Int)
zipTrees (b :> [])       (i :> _)        = …
zipTrees (b :> _)        (i :> [])       = …
zipTrees (b :> (cb:cbs)) (i :> (ci:cis)) = …

or just use zipWith 或只使用zipWith

zipTrees :: Rose Board -> Rose Int -> Rose (Board, Int)
zipTrees (b :> chib) (i :> chii) = (b, i) :> zipWith zipTrees chib chii
--                                           ^^^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM