Recursive set union: how does it work really?

前端 未结 6 2040
梦谈多话
梦谈多话 2020-12-13 14:29

I am currently taking the Scala course on Coursera on my free time after work, in an attempt to finally give a try to functional programming. I am currently working on an as

6条回答
  •  暖寄归人
    2020-12-13 14:59

      2
     / \  union  4
    1   3
    
    ((1 union 3) union 4) incl 2
      ^^^^^^^^^......................................assume it works
    
    (((E union E) union 3 incl 1) union 4) incl 2
       ^^^^^^^^^.....................................still E
    
    (E union E) union 3 incl 1 = E union 3 incl 1 = 3 incl 1
    

    The following subtree should be 3 incl 1

    (  3             ) 
    (    \   union D ) incl 2
    (      1         )
    
    
    (((1 union E) union 4) incl 3) incl 2
       ^^^^^^^^^.......................................expand
    
    (((( (E union E) union E) incl 1) union 4) incl 3) incl 2
          ^^^^^^^^^^^^^^^^^^^^^^^^^^..................still 1
    
    ((1 union 4) incl 3) incl 2
       ^^^^^^^^......................................continue
    
    ((((E union E) union 4) incl 1) incl 3) incl 2
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^..........expand 1 union 4
    
    ((4 incl 1) incl 3) incl 2
      ^^^^^^^^^^^^^^^^^^^^^^^^^............Final union result 
    

    Thanks @Rex Kerr draws out the steps. I substitute the second step with the actual runtime step, which may give a more clear description of the Scala union function.

提交回复
热议问题