I am facing problem with writing quicksort in erlang. What I am doing is I am spawning two processes and then blocking the current process till I get the response from both the left and right sub-arrays. Getting both these responses I send a message to its parent giving it the computed list. Parent ! {self(), Lone ++ [H] ++ Ltwo}
But I am getting error of gettting undef in both the sub-processes. Here is the code.
22 quick(Parent, []) -> Parent ! {self(), []};
23 quick(Parent, [H | T]) ->
24 Pone = spawn_link(main, quick, [ self(), [ X || X <- T, H >= X ] ]) ,
25 Ptwo = spawn_link(main, quick, [ self(), [ Y || Y <- T, H < Y ] ]) ,
26 receive
27 {Pone, Lone} ->
28 receive
29 {Ptwo, Ltwo} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
30 end;
31 {Ptwo, Ltwo} ->
32 receive
33 {Pone, Lone} -> Parent ! {self(), Lone ++ [H] ++ Ltwo}
34 end
35 end.
36
37 sortquick(List) ->
38 quick(self(), List).
called as:
main:sortquick([12,4,7,22,25]).