<div dir="ltr">maybe this (almost equivalent) code will give output with more clear apply order. I tried to draw how perms() calls itself. Hope it helps to understand:<div><div><font face="courier new, monospace">perms(L) -></font></div>
<div><font face="courier new, monospace"> perms("", L).</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">perms(_Prefix, []) -> [[]];</font></div>
<div><font face="courier new, monospace">perms(Prefix, L) -></font></div><div><font face="courier new, monospace"> ListOfPermLists = [perms(Prefix, L, H, L -- [H]) || H <- L],</font></div><div><font face="courier new, monospace"> lists:merge(ListOfPermLists).</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">perms(Prefix, L, H, Others) -></font></div><div><div><font face="courier new, monospace"> io:format("~s-L = ~p, [H] = ~p, Others = ~p~n", [Prefix, L, [H], Others]),</font></div>
<div><font face="courier new, monospace"> OthersPerms = perms(" |" ++ Prefix, Others),</font></div><div><font face="courier new, monospace"> Emit = [ [H|T] || T <- OthersPerms],</font></div><div><font face="courier new, monospace"> Emitted = [io_lib:format("~p ", [E]) || E <- Emit],</font></div>
<div><font face="courier new, monospace"> io:format("~s L = ~p, [H] = ~p, T <- ~p >>> emit ~s~n", [Prefix, L, [H], OthersPerms, Emitted]),</font></div><div><font face="courier new, monospace"> Emit.</font></div>
</div></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/3/4 Ivan Carmenates García <span dir="ltr"><<a href="mailto:co7eb@frcuba.co.cu" target="_blank">co7eb@frcuba.co.cu</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="ES" link="blue" vlink="purple"><div><p class="MsoNormal"><span lang="EN-US">Hi all, I’m reading the Making Reliable Distributed Systems in the Presence of Software Errors by Joe Armstrong Thesis of 2003 and I found this little algorithm using list of compression and there is no way I can understand how internally it work.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:"Courier New"">perms([]) -> [[]];<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:14.5pt;font-family:CMTT12"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I did figure out that the recursion does first I mean the call to the function T <- perms(L--[H]) but step by step I can not follow.<u></u><u></u></span></p><p class="MsoNormal">
<span lang="EN-US">First, the function is called using [1,2,3] list so <u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">[[1|T] || H <- L, T <- perms([1,2,3]--[1] = [2,3])]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">[[1|[2|T]] || H <- L, T <- perms([2,3]--[2] = [3])]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">[[1|[2|[3|T]]] || H <- L, T <- perms([3]--[3] = [])]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">[[1|[2|[3|[]]]] || H <- L, T <- [])]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">And now what! What’s the next step?<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US">I changed the code in order to print the steps so I can understand but even with that the debug is so confuse. Because the order changes, maybe because of the recursive thing.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">But I don’t understand why in the head the value 1 is repeated to yield the next value [[1,2,3), [1,3,2], …]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">perms([]) -> [[]];<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">perms(L) -><u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> io:format("L = ~p~n", [L]),<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> [[{H,io:format("H = ~p - ", [H])}| {T, io:format("T = ~p~n", [T])}] || H <- L, T <- perms(L--[H])].<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span lang="EN-US">This is the exit<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">74> test:perms([1,2,3]).<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [1,2,3]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [2,3]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [3]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 3 - T = []<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 2 - T = [{3,ok}|{[],ok}]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [2]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 2 - T = []<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 3 - T = [{2,ok}|{[],ok}]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 1 - T = [{2,ok}|{[{3,ok}|{[],ok}],ok}]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 1 - T = [{3,ok}|{[{2,ok}|{[],ok}],ok}]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [1,3]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [3]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 3 - T = []<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 1 - T = [{3,ok}|{[],ok}]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [1]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 1 - T = []<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 3 - T = [{1,ok}|{[],ok}]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 2 - T = [{1,ok}|{[{3,ok}|{[],ok}],ok}]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 2 - T = [{3,ok}|{[{1,ok}|{[],ok}],ok}]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [1,2]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [2]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 2 - T = []<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 1 - T = [{2,ok}|{[],ok}]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">L = [1]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 1 - T = []<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 2 - T = [{1,ok}|{[],ok}]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 3 - T = [{1,ok}|{[{2,ok}|{[],ok}],ok}]<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">H = 3 - T = [{2,ok}|{[{1,ok}|{[],ok}],ok}]<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New"">[[{1,ok}|{[{2,ok}|{[{3,ok}|{[],ok}],ok}],ok}],<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> [{1,ok}|{[{3,ok}|{[{2,ok}|{[],ok}],ok}],ok}],<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> [{2,ok}|{[{1,ok}|{[{3,ok}|{[],ok}],ok}],ok}],<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> [{2,ok}|{[{3,ok}|{[{1,ok}|{[],ok}],ok}],ok}],<u></u><u></u></span></p><p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> [{3,ok}|{[{1,ok}|{[{2,ok}|{[],ok}],ok}],ok}],<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""> [{3,ok}|{[{2,ok}|{[{1,ok}|{[],ok}],ok}],ok}]]<u></u><u></u></span></p></div></div><br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><font face="'courier new', monospace">---------------------------------------------</font><div><font face="'courier new', monospace">Данил Загоскин | +7 906 064 20 47 | <a href="mailto:z@gosk.in" target="_blank">z@gosk.in</a></font></div>
</div>