[erlang-questions] Beginner question

Richard A. O'Keefe ok@REDACTED
Wed Mar 16 01:27:41 CET 2016



On 15/03/16 11:53 am, Pietro wrote:
> -module(test).
> -export([start/0, interact/2]).
>
>
> start() ->
>      spawn (fun() -> startloop() end).
>      
>
> interact(Pid, Request) ->
>      Pid ! {self(), Request},
>      receive
> 	{Pid, Response} -> Response
>      end.

Here you expect the process you are interacting with to
return {Pid,Response} pairs, where the first element of
the tuple is that process's own Pid.
>
>
> startloop() ->
>      TableId = ets:new(dictionary, [set]),
>      loop(TableId).
>
> loop(Table) ->
>      receive
> 	{From, {insert, key, value}} ->
> 	    From ! ets:insert(Table, {key, value}),

Here you do NOT send back what's expected, which is
{self(), ets:insert(Table, {key,value})}

I suspect that this clause should have been
         {From, {insert,Key,Value}} ->
             From ! {self(), ets:insert(Table, {Key,Value})},
             loop(Table)
> 	    loop(Table);
> 	{From, {lookup, key}} ->
> 	    From ! ets:lookup(Table, key),

Here you do NOT send back what's expected, which is
{self(), ets:lookup(Table, key)}

I suspect that this clause should have been
         {From, {lookup,Key}} ->
             From ! {self(), ets:lookup(Table, Key)},
             loop(Table)
> 	    loop(Table);
> 	Any ->
> 	    Any
> 		
>      end.
>
>
> The problem happens when I try to interact with the server I start using
> the command:
>
> Pid = test:start().
>
> Then I run :
>
> test:interact(Pid, {insert, testkey, testvalue}).

And here my suspicion is confirmed.

Problem 1: Your responses did not conform to your intended protocol.

Problem 2: You had constants (key) where you meant to have variables (Key),

Your question is fine.  These are slips anyone could have made.
One thing would have helped to avoid them both, and that is
describing the protocol of the new process in a comment.




More information about the erlang-questions mailing list