[erlang-questions] Problem running code from Armstrong's Programming Erlang

David Cabana dcabana@REDACTED
Sat Jun 16 15:56:16 CEST 2007


I am reading a beta copy of Joe Armstrong's "Programming Erlang."   
The book's support site provides code samples for download; I have a  
question regarding a pair of similar bits of code from the site. One  
behaves as I would expect, the other locks up my erl session. I am  
trying to understand why the latter happens.

Here is the first example, the one that works as expected. It is  
available at "http://media.pragprog.com/titles/jaerlang/code/ 
area_server_final.erl".  I have omitted some boilerplate comments at  
the beginning of the example files, for the sake of brevity.

%% ---------------  begin first example
-module(area_server_final).
-export([start/0, area/2]).

start() -> spawn(fun loop/0).

area(Pid, What) ->
     rpc(Pid, What).

rpc(Pid, Request) ->
     Pid ! {self(), Request},
     receive
	{Pid, Response} ->
	    Response
     end.


loop() ->
     receive
	{From, {rectangle, Width, Ht}} ->
	    From ! {self(), Width * Ht},
	    loop();
	{From, {circle, R}} ->
	    From !  {self(), 3.14159 * R * R},
	    loop();
	{From, Other} ->
	    From ! {self(), {error,Other}},
	    loop()
     end.
%% ---------------- end first example

Here is the expected behavior, taken from an erl session:
%% -------------- begin behavior, first example

Eshell V5.5.4  (abort with ^G)

1>c(area_server_final).
{ok,area_server_final}

2> Pid = area_server_final:start().
<0.42.0>

3> area_server_final:area(Pid, {rectangle,2,3}).
6

4>

%% -------------- end behavior, first example
In particular, the area is computed and printed, and control returns  
to erl's read-eval-print loop.

The second example is one that Mr Armstrong provides as a template  
for exploratory programming. I tried to use it as such, with no luck,  
and am trying to understand what is going wrong. The code is  
available at http://media.pragprog.com/titles/jaerlang/code/ 
ctemplate.erl
%% --------------- begin second example

-module(ctemplate).
-compile(export_all).

start() ->
     spawn(fun() -> loop([]) end).

rpc(Pid, Request) ->
     Pid ! {self(), Request},
     receive
	{Pid, Response} ->
	    Response
     end.
	
loop(X) ->
     receive
	Any ->
	    io:format("Received:~p~n",[Any]),
	    loop(X)
     end.

%% --------------- end second example

Notice that the second example is very similar to the first. It's  
purpose is to provide some scaffolding into which the user can  
interactively insert new messages and message handling code.  When I  
try to use it, my session hangs. Here's the terminal session,  
starting with a new erl session.

%% --------------- begin second example behavior

Eshell V5.5.4  (abort with ^G)
1> c(ctemplate).
{ok,ctemplate}

2> Pid = ctemplate:start().
<0.38.0>

3> ctemplate:rpc(Pid, {foo}).
Received:{<0.31.0>,{foo}}

%% --------------- end second example behavior

The erl session hangs at that point. I suspect user error, but have  
no clue what the error might be. I'm brand new to Erlang, and would  
much appreciate any insight on what's going wrong here. Thank you.




More information about the erlang-questions mailing list