Advertisement
Guest User

Untitled

a guest
Aug 21st, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Prova where
  2.  
  3. import Data.Tree
  4.  
  5. annotate :: b -> (b -> a -> b) -> Tree a -> Tree b
  6. annotate k f (Node a sub) =
  7.         let k' = f k a
  8.        in Node k' (map (annotate k' f) sub)
  9.  
  10. treeIn :: Tree Integer
  11. treeIn = Node 7 [Node 3 [], Node 1 [Node 2 [], Node 18 []]]
  12.  
  13. treeOut :: Tree (Integer, Integer)
  14. treeOut = annotate (0,0) (\(a, k) b -> (b, k+b)) treeIn
  15.  
  16. -- treeOut is now a Tree with a “running sum” annotation
  17. -- Node (7,7) [Node (3,10) [], Node (1,8) [Node (2,10) [], Node (18,26) []]]
  18.  
  19. -- Original Q: is it possible to do this with some typeclass (Foldable?
  20. -- Traversable?)
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement