Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 598

OCaml function with data type tree

$
0
0

We are give a tree that contains two types of elements. It is defined with the following data-structure.

type ( 'a , 'b ) tree =   Empty   | Vertexa of 'a * ( 'a , 'b ) tree list   | Vertexb of 'b * ( 'a , 'b ) tree list

Write a function split: ('a,'b) tree ->'a list * 'b list, that saves all elements of type 'a to the first list and all elements of type 'b in the second list.

I had an idea of doing this recursively but I am kind of stuck on this. I will attach my attempt even though it does not work at all :/

let rec one drevo_list=    match drevo_list with   | [Empty]->Empty    | Empty::tl -> one tl   | Vertexa(a,b)::tl -> Vertexa(a,b@tl)    | Vertexb(a,b)::tl -> Vertexb(a,b@tl) 

This function turns a list into a tree. I needed it for the recursion, since the second parametar in Vertexa or Vertexb is a list. And this worksBut the recursion part does not.

let rec split drevo=   match drevo with   | Empty -> [],[]   | Vertexa(a,b)-> split (one b)   | Vertexb(a,b)-> split (one b)

This part does not work and I have no idea how to finish it. Does any one have an idea how to finish this?


Viewing all articles
Browse latest Browse all 598

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>