[erlang-questions] Beginner question

Pravin Gohite <>
Tue Mar 15 23:05:46 CET 2016


Hi,

Your receive for interact function is trying to match Pid along with
Response:


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

loop(Table) ->
    receive
        {From, {insert, key, value}} ->
            From ! ets:insert(Table, {Key, Value}),  %% << you are not
sending something to match with Pid
            loop(Table);
        {From, {lookup, key}} ->
            From ! ets:lookup(Table, Key), %% << you are not sending
something to match with Pid
            loop(Table);
        Any ->
            Any
    end.

So I think this correct send code should be:

>From ! {*self(),* ets:insert(Table, {Key, Value})}

and

>From ! {*self(), *ets:lookup(Table, Key)},

Thanks,
Pravin

On Tue, Mar 15, 2016 at 1:28 PM, Pietro <> wrote:

>  writes:
>
> > Hello,
> >
> > You are using atoms instead of variables in your program. Erlang
> > variables starts with capital letter.
> >
> > The receive loop cannot match your message:
> >
> >     {From, {insert, key, value}} ->
> >
> > It expects one triple of atoms but you send different one.
> >
> >     test:interact(Pid, {insert, testkey, testvalue}).
> >
> > Dmitry
> >
> > Sent from my iPhone
> Ouch ! Thank you very much, I knew I were missing something crucial
> here, would you help me in my next step ?
>
> loop(Table) ->
>     receive
>         {From, {insert, Key, Value}} ->
>             io:format("Here insert"),
>             Return = ets:insert(Table, {Key, Value}),
>             From ! Return,
>             loop(Table);
>         {From, {lookup, Key}} ->
>             io:format("Here lookup"),
>             Return = ets:lookup(Table, Key),
>             From ! Return,
>             loop(Table);
>         {From, Any} ->
>             From ! {self(), {error,Any}},
>             loop(Table)
>     end.
>
> This code results in :
>
> 1> c(test).
> {ok,test}
> 2> Pid = test:start().
> <0.40.0>
> 3> Pid ! {self(), {insert, a, 1}}.
> Here insert{<0.33.0>,{insert,a,1}}
> 4> Pid ! {self(), {lookup, a}}.
> Here lookup{<0.33.0>,{lookup,a}}
>
>
> It does not return the the stored values even though it clearly matches
> the patterns, where is the rub here ?
>
>
> >
> > On 15 Mar 2016, at 00:53, Pietro <> wrote:
> >
> >     Hi all,
> >
> >     I have recently started to implement a small academic project in
> >     Erlang
> >     and I have bumped into a problem which seems to show to myself I
> >     haven't
> >     grasped something important about the technology itself.
> >
> >     That's my code :
> >
> >     -module(test).
> >     -export([start/0, interact/2]).
> >
> >
> >     start() ->
> >     spawn (fun() -> startloop() end).
> >
> >
> >     interact(Pid, Request) ->
> >     Pid ! {self(), Request},
> >     receive
> >     {Pid, Response} -> Response
> >     end.
> >
> >
> >     startloop() ->
> >     TableId = ets:new(dictionary, [set]),
> >     loop(TableId).
> >
> >     loop(Table) ->
> >     receive
> >     {From, {insert, key, value}} ->
> >     From ! ets:insert(Table, {key, value}),
> >     loop(Table);
> >     {From, {lookup, key}} ->
> >     From ! ets:lookup(Table, key),
> >     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}).
> >
> >     My ershell at this point hangs and nothing happens ... what is
> >     happening
> >     ? Please feel free to redirect me to a more appropriate newsgroup
> >     if my
> >     question is not appropriate or if there is a better one.
> >
> >     Thanks in advance.
> >
> >     Pietro.
> >
> >     _______________________________________________
> >     erlang-questions mailing list
> >     
> >     http://erlang.org/mailman/listinfo/erlang-questions
> >
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160315/a56806b7/attachment.html>


More information about the erlang-questions mailing list