Вот моя версия после ответа:
-module(reverse).
-export([reverse/1, join/3, sub_reverse/3]).
reverse("") -> "";
reverse(Str) ->
Step = ceil(string:length(Str) / 10),
SplitPoints = lists:seq(0, string:length(Str) - 1, Step),
JoinPid = spawn(reverse, join, [self(), [], length(SplitPoints)]),
lists:foreach(
fun (Start) ->
IndexPos = Start div Step,
SubStr = string:slice(Str, Start, Step),
spawn(reverse, sub_reverse, [JoinPid, IndexPos, SubStr])
end,
SplitPoints),
receive
{ joined, ReversedStr } -> io:format("~p", [ReversedStr])
end.
join(ReversePid, Acc, N) ->
case length(Acc) of
N ->
Parts = lists:sort(fun ({A, _}, {B, _}) -> A > B end, Acc),
Str = string:join(lists:map(fun ({_, SubStr}) -> SubStr end, Parts), ""),
ReversePid ! { joined, Str };
_ ->
receive
{reversed, X} -> join(ReversePid, [X | Acc], N)
end
end.
sub_reverse(JoinPid, IndexPos, SubStr) ->
JoinPid ! {reversed, { IndexPos, string:reverse(SubStr) } }.