<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi Craig,<br>
<br>
Thanks so much!!<br>
<br>
I usually work to compile programs and when I tried this based on
your code (see source below), it almost works. I must be making a
simple newbie mistake.<br>
<br>
The output I get is:<br>
<br>
<0.58.0>: X is 1000<br>
I don't understand {<0.5.0>,{add,1}}<br>
<0.58.0>: X is 1000<br>
<br>
============<br>
<br>
Here's the code:<br>
<br>
<div style="color: #ffffff;background-color: #002451;font-family: Consolas, 'Courier New', monospace;font-weight: normal;font-size: 16px;line-height: 22px;white-space: pre;"><div><span style="color: #ffffff;">-</span><span style="color: #ebbbff;">module</span><span style="color: #ffffff;">(</span><span style="color: #ffeead;">simple</span><span style="color: #ffffff;">).</span></div><div><span style="color: #ffffff;">-</span><span style="color: #ebbbff;">export</span><span style="color: #ffffff;">([</span><span style="color: #bbdaff;">main</span><span style="color: #ffffff;">/</span><span style="color: #ffc58f;">0</span><span style="color: #ffffff;">]).</span></div><div><span style="color: #ffffff;">-</span><span style="color: #ebbbff;">export</span><span style="color: #ffffff;">([</span><span style="color: #bbdaff;">start</span><span style="color: #ffffff;">/</span><span style="color: #ffc58f;">1</span><span style="color: #ffffff;">, </span><span style="color: #bbdaff;">stop</span><span style="color: #ffffff;">/</span><span style="color: #ffc58f;">1</span><span style="color: #ffffff;">]).</span></div><div><span style="color: #ffffff;">-</span><span style="color: #ebbbff;">export</span><span style="color: #ffffff;">([</span><span style="color: #bbdaff;">add</span><span style="color: #ffffff;">/</span><span style="color: #ffc58f;">2</span><span style="color: #ffffff;">, </span><span style="color: #bbdaff;">sub</span><span style="color: #ffffff;">/</span><span style="color: #ffc58f;">2</span><span style="color: #ffffff;">]).</span></div>
<div><span style="color: #bbdaff;">main</span><span style="color: #ffffff;">() </span><span style="color: #99ffff;">-></span></div>
<div><span style="color: #ff9da4;">Pid</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #bbdaff;">start(</span><span style="color: #ffc58f;">1000</span><span style="color: #bbdaff;">)</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ff9da4;">Pid</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">!</span><span style="color: #ffffff;"> {</span><span style="color: #bbdaff;">self()</span><span style="color: #ffffff;">, {</span><span style="color: #d1f1a9;">add</span><span style="color: #ffffff;">, </span><span style="color: #ffc58f;">1</span><span style="color: #ffffff;">}}.</span></div>
<div><span style="color: #7285b7;">%-----------------------------------------------------------------------------------</span></div>
<div><span style="color: #bbdaff;">start</span><span style="color: #ffffff;">(</span><span style="color: #ff9da4;">X</span><span style="color: #ffffff;">) </span><span style="color: #99ffff;">-></span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">Pid</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #bbdaff;">spawn(</span><span style="color: #ebbbff;">fun</span><span style="color: #bbdaff;">() -> loop(</span><span style="color: #ff9da4;">X</span><span style="color: #bbdaff;">) </span><span style="color: #ebbbff;">end</span><span style="color: #bbdaff;">)</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">Pid</span><span style="color: #ffffff;">.</span></div>
<div><span style="color: #7285b7;">%-----------------------------------------------------------------------------------</span></div>
<div><span style="color: #bbdaff;">loop</span><span style="color: #ffffff;">(</span><span style="color: #ff9da4;">X</span><span style="color: #ffffff;">) </span><span style="color: #99ffff;">-></span></div><div><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">ok</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #ffeead;">io</span><span style="color: #bbdaff;">:format(</span><span style="color: #d1f1a9;">"</span><span style="color: #ffffff;">~p</span><span style="color: #d1f1a9;">: X is </span><span style="color: #ffffff;">~p~n</span><span style="color: #d1f1a9;">"</span><span style="color: #bbdaff;">, [self(), </span><span style="color: #ff9da4;">X</span><span style="color: #bbdaff;">])</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #ebbbff;">receive</span></div><div><span style="color: #ffffff;"> {</span><span style="color: #d1f1a9;">add</span><span style="color: #ffffff;">, </span><span style="color: #ff9da4;">Y</span><span style="color: #ffffff;">} -></span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">NewX</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">X</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">+</span><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">Y</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #bbdaff;">loop(</span><span style="color: #ff9da4;">NewX</span><span style="color: #bbdaff;">)</span><span style="color: #ffffff;">;</span></div><div><span style="color: #ffffff;"> {</span><span style="color: #d1f1a9;">sub</span><span style="color: #ffffff;">, </span><span style="color: #ff9da4;">Y</span><span style="color: #ffffff;">} -></span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">NewX</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">X</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">-</span><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">Y</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #bbdaff;">loop(</span><span style="color: #ff9da4;">NewX</span><span style="color: #bbdaff;">)</span><span style="color: #ffffff;">;</span></div><div><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">stop</span><span style="color: #ffffff;"> -></span></div><div><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">ok</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #ffeead;">io</span><span style="color: #bbdaff;">:format(</span><span style="color: #d1f1a9;">"Bye!</span><span style="color: #ffffff;">~n</span><span style="color: #d1f1a9;">"</span><span style="color: #bbdaff;">)</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #bbdaff;">exit(</span><span style="color: #d1f1a9;">normal</span><span style="color: #bbdaff;">)</span><span style="color: #ffffff;">;</span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">Unexpected</span><span style="color: #ffffff;"> -></span></div><div><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">ok</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">=</span><span style="color: #ffffff;"> </span><span style="color: #ffeead;">io</span><span style="color: #bbdaff;">:format(</span><span style="color: #d1f1a9;">"I don't understand </span><span style="color: #ffffff;">~t</span><span style="color: #d1f1a9;">p</span><span style="color: #ffffff;">~n</span><span style="color: #d1f1a9;">"</span><span style="color: #bbdaff;">, [</span><span style="color: #ff9da4;">Unexpected</span><span style="color: #bbdaff;">])</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #bbdaff;">loop(</span><span style="color: #ff9da4;">X</span><span style="color: #bbdaff;">)</span></div><div><span style="color: #ffffff;"> </span><span style="color: #ebbbff;">end</span><span style="color: #ffffff;">.</span></div><div><span style="color: #ffffff;"> </span></div><div><span style="color: #7285b7;">%-----------------------------------------------------------------------------------</span></div>
<div><span style="color: #bbdaff;">add</span><span style="color: #ffffff;">(</span><span style="color: #ff9da4;">PID</span><span style="color: #ffffff;">, </span><span style="color: #ff9da4;">Amount</span><span style="color: #ffffff;">) </span><span style="color: #99ffff;">-></span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">PID</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">!</span><span style="color: #ffffff;"> {</span><span style="color: #d1f1a9;">add</span><span style="color: #ffffff;">, </span><span style="color: #ff9da4;">Amount</span><span style="color: #ffffff;">},</span></div><div><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">ok</span><span style="color: #ffffff;">.</span></div>
<div><span style="color: #bbdaff;">sub</span><span style="color: #ffffff;">(</span><span style="color: #ff9da4;">PID</span><span style="color: #ffffff;">, </span><span style="color: #ff9da4;">Amount</span><span style="color: #ffffff;">) </span><span style="color: #99ffff;">-></span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">PID</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">!</span><span style="color: #ffffff;"> {</span><span style="color: #d1f1a9;">sub</span><span style="color: #ffffff;">, </span><span style="color: #ff9da4;">Amount</span><span style="color: #ffffff;">},</span></div><div><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">ok</span><span style="color: #ffffff;">.</span></div>
<div><span style="color: #bbdaff;">stop</span><span style="color: #ffffff;">(</span><span style="color: #ff9da4;">PID</span><span style="color: #ffffff;">) </span><span style="color: #99ffff;">-></span></div><div><span style="color: #ffffff;"> </span><span style="color: #ff9da4;">PID</span><span style="color: #ffffff;"> </span><span style="color: #99ffff;">!</span><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">stop</span><span style="color: #ffffff;">,</span></div><div><span style="color: #ffffff;"> </span><span style="color: #d1f1a9;">ok</span><span style="color: #ffffff;">.</span></div>
<div><span style="color: #7285b7;">%-----------------------------------------------------------------------------------</span></div></div>
<br>
<br>
<div class="moz-cite-prefix">On 12/23/2018 9.24 PM, <a class="moz-txt-link-abbreviated" href="mailto:zxq9@zxq9.com">zxq9@zxq9.com</a>
wrote:<br>
</div>
<blockquote type="cite" cite="mid:4272990.rzK7TuVrv7@takoyaki">
<pre class="moz-quote-pre" wrap="">-module(simple).
-export([start/1, stop/1]).
-export([add/2, sub/2]).
start(X) ->
spawn(fun() -> loop(X) end).
add(PID, Amount) ->
PID ! {add, Amount},
ok.
sub(PID, Amount) ->
PID ! {sub, Amount},
ok.
stop(PID) ->
PID ! stop,
ok.
loop(X) ->
ok = io:format("~p: X is ~p~n", [self(), X]),
receive
{add, Y} ->
NewX = X + Y,
loop(NewX);
{sub, Y} ->
NewX = X - Y,
loop(NewX);
stop ->
ok = io:format("Bye!~n"),
exit(normal);
Unexpected ->
ok = io:format("I don't understand ~tp~n", [Unexpected]),
loop(X)
end.</pre>
</blockquote>
<br>
</body>
</html>