I am trying to code a tree type with nodes containing different types of value and functions to map to different formats while keeping the structure of the tree. Below is an example with three types and function to convert the tree with “string, int, bool” to a tree of only string.
type ('a, 'b, 'c) node = | StringLeaf of 'a | IntLeaf of 'b | Node of 'c * ('a, 'b, 'c) node list let rec string_of_node = function | StringLeaf v -> StringLeaf v | IntLeaf v -> IntLeaf (string_of_int v) | Node (v, children) -> Node (string_of_bool v, List.map string_of_node children) let string_tree = string_of_node (Node (true, [IntLeaf 5; StringLeaf "hello"]))
The approach works for small example, but becomes tedious when the number of parametric types and number of node types increase. Is there a better way to write this code? Thanks in advance.