Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to define trees with more than one type in ML programing language
    primarykey
    data
    text
    <p>Well, I am asked to do the next thing:</p> <p>To define a binary tree which can contain 2 different types: ('a,'b) abtree and these are the requirements:</p> <ol> <li>Any inner vertex (not a leaf) must be of the type 'a or 'b and the leafs have no value.</li> <li><p>For every path in the tree all 'a values must appear before the 'b value: examples of paths:</p> <pre><code>'a-&gt;'a-&gt;'a-'b (legal) 'a-&gt;'b-&gt;'b (legal) 'a-&gt;'a-&gt;'a (legal) 'b-&gt;'b-&gt;'b (legal) 'a-&gt;'b-&gt;'a (ILLEGAL) </code></pre></li> </ol> <p>and also I need to define another tree which is like the one described above but now I have got also 'c and in the second requirement it says that for every path I 'a values appear before the 'b values and all the 'b values appear before the 'c values.</p> <p>First, I am not sure how to define binary trees to have more than 1 type in them. I mean the simplest binary tree is:</p> <pre><code>datatype 'a tree = leaf | br of 'a * 'a tree * 'a tree; </code></pre> <p>And also how I can define a tree to have these requirements.</p> <p>Any help will be appreciated.</p> <p>Thanks.</p> <hr> <p>OK, thanks a lot. So you mean something like that:</p> <pre><code>datatype 'b bTree = leaf | bBranch of 'b * 'b bTree * 'b bTree ; datatype ('a,'b) abTree = leaf | aBranch of 'a * ('a, 'b) abTree * ('a,'b) abTree | bBranch of 'b * 'b bTree * 'b bTree ; </code></pre> <p>and that's what I did for the case it's a 3 type tree as I mentioned above:</p> <pre><code>datatype 'c cTree = leaf | cBranch of 'c * 'c cTree * 'c cTree ; datatype ('b, 'c) bcTree = leaf | bBranch of 'b * ('b, 'c) bcTree * ('b,'c) bcTree | cBranch of 'c * 'c cTree * 'c cTree ; datatype ('a, 'b, 'c) abcTree = leaf | aBranch of 'a * ('a, 'b, 'c) abcTree * ('a, 'b, 'c) abcTree | bBranch of 'b * ('b, 'c) bcTree * ('b, 'c) bcTree | cBranch of 'c * 'c cTree * 'c cTree ; </code></pre> <p>Am I right?</p> <p>Also, what does the requirement of leafs means? The one that says that the leafs should have no value?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload