<?xml version="1.0"?>
<paste-with-annotations>
  <paste>
    <number>
      <integer>26025</integer>
    </number>
    <user>
      <string>rhymes</string>
    </user>
    <title>
      <string>ring excercise with comments</string>
    </title>
    <contents>
      <string>-module(star).
-export([go/3, build_ring/2, proc/0, send/4]).

%% recreates this: http://www.erlang.org/course/ex3.gif

% build a list of pids while starting the processes
% build(N, L) where N is the number of processes to spawn
% and L the list to append to
build_ring(0, L) -&gt;
    L;
build_ring(N, L) -&gt;
    List = lists:append(L, [spawn(star, proc, [])]),
    build_ring(N - 1, List).

% send the message across the ring surrounding the process
% in the center (the main process). If the list of processes
% gets empty a loop is done so start again. When the count
% of bounces reaches 0 we're done. 
send(_, _, 0, _) -&gt; 
    exit(normal);
send(Msg, [], N, Orig_Pid_List) -&gt;
    send(Msg, Orig_Pid_List, N, Orig_Pid_List);
send(Msg, [Next_PID | Rest], N, Orig_Pid_List) -&gt;
    Next_PID ! {self(), Msg},
    io:format(&quot;sent to ~w~n&quot;, [Next_PID]),
    receive
        {Pid, Msg} -&gt;
            io:format(&quot;received from ~w~n&quot;, [Pid]),
            send(Msg, Rest, N - 1, Orig_Pid_List)
    end.

% proc identifies the node, it only has to bounce the message
% and go back to listen
proc() -&gt;
    receive
        {Pid, Msg} -&gt;
            Pid ! {self(), Msg},
            proc()
    end.

% the entry point of the program.
go(Num_Processes, Num_Bounces, Msg) -&gt;
    Pid_List = build_ring(Num_Processes, []),
    io:format(&quot;built ~w processes.~n&quot;, [Num_Processes]),
    send(Msg, Pid_List, Num_Bounces, Pid_List).

</string>
    </contents>
    <universal-time>
      <integer>3367228216</integer>
    </universal-time>
    <channel>
      <string>None</string>
    </channel>
    <colorization-mode>
      <string>Erlang</string>
    </colorization-mode>
    <maybe-spam>
      <null/>
    </maybe-spam>
    <is-unicode>
      <null/>
    </is-unicode>
  </paste>
  <annotation>
    <number>
      <integer>1</integer>
    </number>
    <user>
      <string>rhymes</string>
    </user>
    <title>
      <string>star example revisited</string>
    </title>
    <contents>
      <string>%% now it's exactly as the example #3 here:
%% http://www.erlang.org/course/exercises.html#conc

-Module(star2).
-export([go/3, build_ring/2, proc/0, send/4]).

%% Write a function which starts N processes in a star, and sends a message to
%% each of them M times. After the messages have been sent the processes
%% should terminate gracefully.

% build a list of pids while starting the processes
% build(N, L) where N is the number of processes to spawn
% and L the list to append to
build_ring(0, L) -&gt;
    L;
build_ring(N, L) -&gt;
    List = lists:append(L, [spawn(star2, proc, [])]),
    build_ring(N - 1, List).

% send the message across the ring surrounding the process
% in the center (the main process). If the list of processes
% gets empty a loop is done so start again. When the count
% of bounces reaches 0 we're done. 
send(_, _, 0, _) -&gt; 
    exit(normal);
send(Msg, [], N, Orig_Pid_List) -&gt;
    send(Msg, Orig_Pid_List, N - 1, Orig_Pid_List);
send(Msg, [Next_PID | Rest], N, Orig_Pid_List) -&gt;
    Next_PID ! {self(), Msg},
    receive
        {Pid, Msg} -&gt;
            io:format(&quot;received from ~w, ~w~n&quot;, [Pid, N]),
            send(Msg, Rest, N, Orig_Pid_List)
    end.

% proc identifies the node, it only has to bounce the message
% and go back to listen
proc() -&gt;
    receive
        {Pid, Msg} -&gt;
            Pid ! {self(), Msg},
            proc()
    end.

% the entry point of the program.
go(Num_Processes, Num_Bounces, Msg) -&gt;
    Pid_List = build_ring(Num_Processes, []),
    io:format(&quot;built ~w processes.~n&quot;, [Num_Processes]),
    send(Msg, Pid_List, Num_Bounces, Pid_List).
</string>
    </contents>
    <universal-time>
      <integer>3367244043</integer>
    </universal-time>
    <channel>
      <string>None</string>
    </channel>
    <colorization-mode>
      <string></string>
    </colorization-mode>
    <maybe-spam>
      <null/>
    </maybe-spam>
    <is-unicode>
      <null/>
    </is-unicode>
  </annotation>
</paste-with-annotations>