Ocaml change type 'a tree to 'b tree -
i have defined
type 'a tree = | tree of 'a tree * 'a * 'a tree | leaf of 'a | null;; now have define function change whole 'a tree in 'b tree.
i have searched on google , tried solve myself i'm stucked. tips?
one possibility analog map function on trees. example:
let rec treemap f t = match t null -> null | leaf x -> leaf (f x) | tree (l, x, r) -> tree (treemap f l, f x, treemap f r) here t : 'a tree, f : 'a -> 'b, , treemap : 'a tree -> ('a -> 'b) -> 'b tree, produces 'b tree desired.
this "natural" way of making such function: have tree values of type 'a, , want new tree values of type 'b - need tree , way of mapping value of 1 type value of other.
i think it's worth pointing out isn't such function. make function treereplace : 'a tree -> 'b -> 'b tree, creates identically structured tree replacing every value 1 given, or function same flips left/right branches of each tree node. there's infinite number of functions taking 'a tree , producing 'b tree make, above arguably natural , useful.
edit: improvements/critique welcome - don't know ocaml, hacked example understanding of sml.
Comments
Post a Comment