1
votes

I'm trying to implement a kind of zip-function for trees. For this example each tree-node has an Integer Value (Label), a transformative function (Label->Label) and a List of sub-trees [Tree].

the tzp (tree-zip) Method takes two trees and adds their values together based on some criteria. Here's the question:

How do I get my function to recursively take the next subtree of each [Tree]-List?

My current implementation in case you're curious

tzp :: (Label -> Label -> Label) -> Tree -> Tree -> Tree
tzp arithmeticF t1 t2 = if ((getNodeValue t1) == (getNodeValue t2)) then (Node (arithmeticF (getNodeValue t1)) ((getNodeFunction t1) . (getNodeFunction t2) (getNodeValue t1)) (map (tzp arithmeticF) (getSubTrees t1) (getSubTrees t2)))

the culprit is this part:

(map (tzp arithmeticF) (getSubTrees t1) (getSubTrees t2))

since map can't work with two lists. To be precise I have to apply the function to the n'th element of either list. So first with first, second with second, etc.

Any help is greatly appreciated!

1
You need to post the definitions of the types you're working with. And you might be looking for zipWith?bheklilr

1 Answers

0
votes
Prelude> :t uncurry
uncurry :: (a -> b -> c) -> (a, b) -> c
Prelude> :t (+)
(+) :: Num a => a -> a -> a
Prelude> :t uncurry (+)
uncurry (+) :: Num c => (c, c) -> c

So, something like:

map (uncurry tzp arithmeticF) (zip (getSubTrees t1) (getSubTrees t2))

Should do the trick.