Question: Please code in OCaml language. Given a working tree_fold function that does exactly as described above in the first image. Please code a map, mirror,

 Please code in OCaml language. Given a working tree_fold function that

does exactly as described above in the first image. Please code a

Please code in OCaml language.

Given a working tree_fold function that does exactly as described above in the first image. Please code a map, mirror, and in_order function using the tree_fold method.

Please do not use any other submodules of Stdlib library, other than the List library module. Also do not use recursion for these 3 functions, and please test to make sure that your code works, preferably using the example provided under the description in the images above.

Thank you.

tree_fold f init tree - Type: (( ' abaa)a ' tree ' a ) - Description: Given a function f, accumulator init, and tree, iterate over the given tree using f and return the iterated value of type 'a. The function f will take in three parameters: the value of the accumulator returned by the left branch of the node, the value of the current node, and the value of the accumulator returned by the right branch of the node, and should then return the new accumulated value of type . - Examples: let treea = Node(Node(Leaf, "Hello", Leaf), " World", Node(Leaf, "!", Leaf)) in let treeb = Node(Node(Leaf, 5 , Leaf), 6, Leaf) in tree_fold (fun 1ssr1sr)" treea = "Hello World!" tree_fold (fun 1rmax(max1x)r treeb =6 map tree f - Type: ('a tree ( ('a 'b) 'b tree) - Description: Given a function f, map all the values of the nodes in the tree using f. You must implement this function using tree_fold . Note that the mapped tree should still return the same tree shape with the corresponding mapped nodes. - Examples: let treea = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 2, Leaf)) in let treeb = Node(Node(Leaf, 1, Leaf), 2, Node(Node(Leaf, 3, Leaf), 4, Leaf)) in map treea string_of_int = Node(Node(Leaf, "1", Leaf), "2", Node(Leaf, "2", Leaf)) map treeb (fun xx+1)= Node(Node(Leaf, 2, Leaf), 3, Node(Node(Leaf, 4, Leaf), 5, Leaf)) mirror tree - Type: ('a tree 'a tree) - Description: Write a function using tree_fold that will return the given tree with the left and right branches swapped at each node. - Examples: let treea =Node(Node( Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)) let treeb =Node(Node( Leaf, 1, Leaf), 2, Node(Node(Leaf, 3, Leaf), 4, Leaf)) mirror treea =Node(Node(Leaf,3,Leaf),2,Node(Leaf,1,Leaf)); mirror treeb =Node(Node(Leaf,4,Node(Leaf,3,Leaf)),2,Node(Leaf,1,Leaf)) in_order tree - Type: ('a tree ' a list) - Description: Using tree_fold, write a function that will return a list containing the in order traversal of the tree. - Examples: let treea = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf) ) let treeb = Node(Node(Leaf, 1, Leaf), 2, Node(Node(Leaf, 3, Leaf), 4, Leaf)) in_order treea =[1;2;3] in_order treeb =[1;2;3;4]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!