How to define a map and collapse to search trees?

I have a search tree that is defined as:

data (Ord a) => Stree a = Null | Fork (Stree a) a (Stree a) deriving Show 

and I need to define two functions: mapStree:

mapStree :: (Ord b, Ord a) => (a -> b) -> Stree a -> Stree b

and foldStree:

foldStree :: (Ord a) => (b -> a -> b -> b) -> b -> Stree a -> b

I do not quite understand what is happening, and I do not know how to do it.

+3
source share
2 answers

You want your map to apply the function to any label carried by your tree. This means that any event amust be changed to occur before busing the function specified as the transform function.

, Stree. Null - a . Trickier - Fork. Fork a Stree, , a -> b Stree a -> Stree b. mapStree , mapStree f ( !).

foldStree b a , b a b, , , , Fork : , , Stree, , a , b, . b foldStree , .

, foldStree : Null, Fork, Stree .

, , : ( ) , , , , .

+5

5 .

+1

Source: https://habr.com/ru/post/1770631/


All Articles