Tim
Tim

Reputation: 13

How to zip Rose Trees in Haskell

For a Haskell Tic Tac Toe game, I am trying to zip two Rose Trees. 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. 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

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

Upvotes: 1

Views: 304

Answers (1)

Bergi
Bergi

Reputation: 664195

chib and chii are lists of trees, not trees themselves. You can't just call zipTrees on them. 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

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

Upvotes: 5

Related Questions