[back]

Mapping Rose Trees

type 'a rtree = Node of 'a * 'a rtree list

Implement the function

val map_rtree : ('a -> 'b) -> 'a rtree -> 'b rtree

so that map_rtree f t is the result of applying f to every value of t, maintaining the structure of t.

let example : int rtree =
  Node
    ( 1
    , [ Node (2, [])
      ; Node (3, [Node (4, []); Node (5, [])])
      ; Node (6, [])
      ]
    )
let _ = assert
  (map_rtree (fun x -> x * x) example
   =
   Node
     ( 1
     , [ Node (4, [])
       ; Node (9, [Node (16, []); Node (25, [])])
       ; Node (36, [])
       ]
     ))