Problem with code !

12 views
Skip to first unread message

!!!@!!!

unread,
Sep 6, 2010, 4:55:11 PM9/6/10
to Erlang Programming
Hi! I'm having problem with this code:

-module(sample).

-export([start/0, loop/0]).

start() ->
Pid = spawn(sample, loop, []),
Pid ! {self(), do_sum_of, 1, 2},
receive
{Pid, Msg} ->
io:format("~w~n", [Msg])
end,
Pid ! stop.

loop() ->
receive
{From, sum, First, Second} ->
From ! {self(), First + Second},
loop();
{From, do_sum_of, First, Second} ->
self() ! {From, sum, First, Second};
stop -> true
end.

When I type "sample:start()." the shell hangs, it doesn't return
anything.
Can anybody explain what the problem is and why does this happen?
Can erlang processes send messages to themselves without crashing?

Ale

unread,
Sep 6, 2010, 5:11:24 PM9/6/10
to erlang-prog...@googlegroups.com


2010/9/6 !!!@!!! <elto...@gmail.com>

Hi! I'm having problem with this code:

-module(sample).

-export([start/0, loop/0]).

start() ->
   Pid = spawn(sample, loop, []),
   Pid ! {self(), do_sum_of, 1, 2},
   receive
       {Pid, Msg} ->
 
 you are waiting on a message with this pattern {Pid, Msg}
 but the message sent has another pattern.
 it has {From, sum, First, Second} pattern
 this is why it hangs, it waits for a message that never comes.
 
           io:format("~w~n", [Msg])
   end,
   Pid ! stop.

loop() ->
   receive
       {From, sum, First, Second} ->
           From ! {self(), First + Second},
           loop();
       {From, do_sum_of, First, Second} ->
           self() ! {From, sum, First, Second};
       stop -> true
end.

When I type "sample:start()." the shell hangs, it doesn't return
anything.
Can anybody explain what the problem is and why does this happen?
Can erlang processes send messages to themselves without crashing?

 
--
Ale.

Yoshi

unread,
Sep 6, 2010, 7:35:37 PM9/6/10
to Erlang Programming


On Sep 6, 1:55 pm, "!!!@!!!" <eltoni...@gmail.com> wrote:
> Hi! I'm having problem with this code:
>
> -module(sample).
>
> -export([start/0, loop/0]).
>
> start() ->
>     Pid = spawn(sample, loop, []),
>     Pid ! {self(), do_sum_of, 1, 2},
>     receive
>         {Pid, Msg} ->
>             io:format("~w~n", [Msg])
>     end,
>     Pid ! stop.
>
> loop() ->
>     receive
>         {From, sum, First, Second} ->
>             From ! {self(), First + Second},
>             loop();
>         {From, do_sum_of, First, Second} ->
>             self() ! {From, sum, First, Second};
Hello, here you are sending message to loop() process, but it seems
not receiving this message.
inserting 'loop();' would fix your problem ? :)

!!!@!!!

unread,
Sep 6, 2010, 8:19:42 PM9/6/10
to Erlang Programming
If you see carefully, first I am sending to the loop() process the
{self(), do_sum_of, 1, 2} message.
This message matches with the second pattern in the receive of the
loop().
The second pattern then sends to itself (the loop() process) the
{From, sum, First, Second} message which is captures by the first
pattern of the loop() process.
The first pattern then sends to start() ( From = start() process pid )
the {self(), First+Secong} message which should match the {Pid, Msg}
pattern in which Pid should be = self() and Msg should be = First
+Second.
I don't see where the problem is???

Note: This is my first time and the third day of learning Erlang and I
have skiped many things!

On Sep 6, 11:11 pm, Ale <peralta.alejan...@gmail.com> wrote:
> 2010/9/6 !!!@!!! <eltoni...@gmail.com>

!!!@!!!

unread,
Sep 6, 2010, 8:43:22 PM9/6/10
to Erlang Programming
I don't get it?
Where should I insert the loop(); ???

Ale

unread,
Sep 6, 2010, 8:51:00 PM9/6/10
to erlang-prog...@googlegroups.com


2010/9/6 !!!@!!! <elto...@gmail.com>

I don't get it?
Where should I insert the loop(); ???

Sorry for misguiding you the first time I didn't fully understand your program. Yoshi is right.

I'll give you a hint, instead of straight answer, hoping you think about it :-)


> > loop() ->
> >     receive
> >         {From, sum, First, Second} ->
> >             From ! {self(), First + Second},
> >             loop();
> >         {From, do_sum_of, First, Second} ->
                 %% here you send a message to the process that's running the loop function
                 %% using self() ! Msg, but then the function and the process, terminate.
                 %% so where should you put the tail-call to loop?

> >             self() ! {From, sum, First, Second};


Cheers,

--
Ale.

!!!@!!!

unread,
Sep 7, 2010, 8:43:33 AM9/7/10
to Erlang Programming
This is my new code, that prints "3" and "stop":

-module(sample).
-export([start/0, loop/0]).
start() ->
Pid = spawn(sample, loop, []),
Pid ! {self(), do_sum_of, 1, 2},
receive
{Pid, Msg} ->
io:format("~p~n", [Msg])
end,
Pid ! stop.
loop() ->
receive
{From, sum, First, Second} ->
From ! {self(), First + Second},
loop();
{From, do_sum_of, First, Second} ->
self() ! {From, sum, First, Second},
loop();
stop -> true
end.

Thanks for the help Yoshi and Ale!!!
Reply all
Reply to author
Forward
0 new messages