scala - Recursive set union: how does it work really? -
i taking scala course on coursera on free time after work, in attempt give try functional programming. working on assignment supposed "calculate" union of 2 sets contain object. intentionally omitting details it's not important trying ask here. relevant, however, sets defined binary trees, each node containing element, , 2 subtrees.
that being case; example union
in lecture follows:
def union(other:btset) :btset = ((left union right) union other) incl element
question1: quite frankly, after having read relevant faq , other forum threads, still don't understand how , why function works. there absolutely no "action" done here in union implementation besides adding (the incl
call) element @ head node, calls on , on again. appreciative of explanation...
question2: course forum contains many posts stating solution not efficient @ all, , not enough. seeing don't understand how works begin don't understand why it's not enough.
please note not, in way, ask spoiler assignment solution. more willing "do work grade" don't understand supposed here. don't believe instructions , guidance provided in course adequate wrap head around quirks of functional programming, welcome comments/answers address how think right rather how code right.
/ \ union d b c ((b union c) union d) incl ^^^^^^^^^......................................assume works ( b ) ( \ union d ) incl ( c ) (((0 union c) union d) incl b) incl ^^^^^^^^^.....................................just c (((c union d) incl b) incl ^^^^^^^^^.....................................expand ((((0 union 0) union d) incl c) incl b) incl ^^^^^^^^^....................................just 0 (((0 union d) incl c) incl b) incl ^^^^^^^^^.....................................just d ((d incl c) incl b) incl ^^^^^^^^^^^^^^^^^^^^^^^^^^.......................all incl
just write out step-by step. see union reduces bunch of incl statements applied right-hand argument.