<div dir="auto"><div class="gmail_quote" dir="auto"><div dir="ltr"><span style="font-family:monospace,monospace">Hello,</span><br></div><div dir="ltr"><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">I am new to Erlang and functional programming (1-1.5 months) and have task:</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1. Suppose we have hierarchical tree-like data structure:</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>L = [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id1", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id1.1", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{policy, "p1", "v1"},</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{policy, "p2", "v2"}</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>]},</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id1.2", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>]}</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>]},</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id2", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{policy, "p3", "v3"}</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>] },</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id3", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>] }</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>]</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">2. <span style="white-space:pre-wrap"> </span>List of some "section"'s with children elements any of which could be</font></div><div><font face="monospace, monospace">another "section" with children or "policy" with name and value.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">3.<span style="white-space:pre-wrap"> </span>Also we have another list which contains identifiers of policies:</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>F = ["p1", "p3"].</font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace"> </font></span></div><div><font face="monospace, monospace">4.<span style="white-space:pre-wrap"> </span>Now I need to write a function which takes lists L, F and returns list L2</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>such as L2 contains only those "section"'s which has "policies" wich Id</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>equals to those in F. Empty sections must be removed.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>For lists L and F result should be:</font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace"> </font></span></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>L2 = [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id1", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id1.1", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{policy, "p1", "v1"},</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>]},</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>]},</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{section, "id2", [</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{policy, "p3", "v3"}</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>] },</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>]</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">5.<span style="white-space:pre-wrap"> </span>I have solution (please see below) but it seems to me not as good as</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>it can be and even little weird. I am sure that it could be improved </font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>and done in proper way.</font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace"> </font></span></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>If anyone could give me any glimpse or advice I would appreciate.</font></div><div><font face="monospace, monospace"><br></font></div><div><span style="white-space:pre-wrap"><font face="monospace, monospace"> </font></span></div><div><font face="monospace, monospace">Source:</font></div><div><font face="monospace, monospace">---------------------------------------------------------------------------</font></div><div><font face="monospace, monospace">main() -></font></div><div><font face="monospace, monospace"> L = [</font></div><div><font face="monospace, monospace"> {section, "id1", [</font></div><div><font face="monospace, monospace"> {section, "id1.1", [</font></div><div><font face="monospace, monospace"> {policy, "p1", "v1"},</font></div><div><font face="monospace, monospace"> {policy, "p2", "v2"}</font></div><div><font face="monospace, monospace"> ]},</font></div><div><font face="monospace, monospace"> {section, "id1.2", [</font></div><div><font face="monospace, monospace"> ]}</font></div><div><font face="monospace, monospace"> ]},</font></div><div><font face="monospace, monospace"> {section, "id2", [</font></div><div><font face="monospace, monospace"> {policy, "p3", "v3"}</font></div><div><font face="monospace, monospace"> ] },</font></div><div><font face="monospace, monospace"> {section, "id3", [</font></div><div><font face="monospace, monospace"> ] }</font></div><div><font face="monospace, monospace"> ],</font></div><div><font face="monospace, monospace"> %filter(L, ["p1", "p3"]).</font></div><div><font face="monospace, monospace"> filter(L, ["p3"]).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">filter([H | T], Search) -></font></div><div><font face="monospace, monospace"> lists:flatten([filter(H, Search, []) | filter(T, Search, [])]).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">filter({section, I, C}, Search, _Acc) -></font></div><div><font face="monospace, monospace"> NewC = filter(C, Search, []),</font></div><div><font face="monospace, monospace"> case NewC of</font></div><div><font face="monospace, monospace"> [] -> [];</font></div><div><font face="monospace, monospace"> _ -> {section, I, NewC}</font></div><div><font face="monospace, monospace"> end;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">filter([{section, I, C} | T], Search, Acc) -></font></div><div><font face="monospace, monospace"> NewC = filter(C, Search, []),</font></div><div><font face="monospace, monospace"> case NewC of</font></div><div><font face="monospace, monospace"> [] -> filter(T, Search, Acc);</font></div><div><font face="monospace, monospace"> _ -> filter(T, Search, [{section, I, NewC} | Acc])</font></div><div><font face="monospace, monospace"> end;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">filter([{policy, I, V} | T], Search, Acc) -></font></div><div><font face="monospace, monospace"> case lists:member(I, Search) of</font></div><div><font face="monospace, monospace"> true -> filter(T, Search, [{policy, I, V} | Acc]);</font></div><div><font face="monospace, monospace"> false -> filter(T, Search, Acc)</font></div><div><font face="monospace, monospace"> end;</font></div><div><font face="monospace, monospace">filter([], _S, Acc) -> Acc.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">-- Stanislav Ledenev<br></font></div></div>
</div></div>