help with message-passing syntax

im using:
http://www.erlang.org/doc/reference_manual/part_frame.html
but cant really figure it out. i didnt expect this to work but compiled anyway:
2> c("c:/Program Files/erl5.6.2/usr/serec", [{outdir, "c:/Program Files/erl5.6.2/usr/"}]).
c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer/1 undefined
c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer2/1 undefined
c:/Program Files/erl5.6.2/usr/serec.erl:23: premature end
error
3>


obv this is just a toy program but say I have a function that squares the integers of a list from 1 to the send parameter.
so i want to send a message to that function.


-module(serec).
-export([seq/2,squarer/1,squarer2/1]).
         
seq(Start, End) -> seq(Start, End, []).

seq(Start, End, Acc) when Start =< End ->
   seq(Start, End-1, [End|Acc]);
seq(_, _, Acc) ->
   Acc.

squarer(X) ->
    receive
    Pattern [when Pattern > 7] ->
        [X*X || X <- [seq(1, 7]];
    end

squarer2(X) ->
    receive
    when X > 7 -> [X*X || X <- [seq(1, 7]];
    end

squarer(X) ! 2+6


Låna pengar utan säkerhet.
Sök och jämför lån hos Kelkoo.
<div>
<table cellspacing="0" cellpadding="0" border="0"><tr><td valign="top">im using:<br>http://www.erlang.org/doc/reference_manual/part_frame.html<br>but cant really figure it out. i didnt expect this to work but compiled anyway:<br>2&gt; c("c:/Program Files/erl5.6.2/usr/serec", [{outdir, "c:/Program Files/erl5.6.2/usr/"}]).<br>c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer/1 undefined<br>c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer2/1 undefined<br>c:/Program Files/erl5.6.2/usr/serec.erl:23: premature end<br>error<br>3&gt; <br><br><br>obv this is just a toy program but say I have a function that squares the integers of a list from 1 to the send parameter.<br>so i want to send a message to that function.<br><br><br>-module(serec).<br>-export([seq/2,squarer/1,squarer2/1]).<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>seq(Start, End) -&gt; seq(Start, End, []).<br><br>seq(Start, End, Acc) when Start
 =&lt; End -&gt;<br>&nbsp;&nbsp; seq(Start, End-1, [End|Acc]);<br>seq(_, _, Acc) -&gt;<br>&nbsp;&nbsp; Acc.<br><br>squarer(X) -&gt;<br>&nbsp;&nbsp;&nbsp; receive<br>&nbsp;&nbsp; &nbsp;Pattern [when Pattern &gt; 7] -&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; [X*X || X &lt;- [seq(1, 7]];<br>&nbsp;&nbsp;&nbsp; end<br><br>squarer2(X) -&gt;<br>&nbsp;&nbsp;&nbsp; receive<br>&nbsp;&nbsp; &nbsp;when X &gt; 7 -&gt; [X*X || X &lt;- [seq(1, 7]];<br>&nbsp;&nbsp;&nbsp; end<br><br>squarer(X) ! 2+6<br><br>
</td></tr></table>
<br>
L&aring;na pengar utan s&auml;kerhet.<br><a href="http://www.kelkoo.se/c-100390123-lan-utan-sakerhet.html?partnerId=96915014">S&ouml;k och j&auml;mf&ouml;r l&aring;n hos Kelkoo.</a>
</div>
David Mercer | 3 Jul 17:04

Re: help with message-passing syntax

You don’t send messages to functions, you send them to processes.  Also, you’re missing periods after the squarer and squarer2 functions (after the ends), which accounts for two of the compiler errors.

 

From: erlang-questions-bounces <at> erlang.org [mailto:erlang-questions-bounces <at> erlang.org] On Behalf Of not norwegian swede
Sent: Thursday, July 03, 2008 04:04
To: erlang-questions <at> erlang.org
Subject: [erlang-questions] help with message-passing syntax

 

im using:
http://www.erlang.org/doc/reference_manual/part_frame.html
but cant really figure it out. i didnt expect this to work but compiled anyway:
2> c("c:/Program Files/erl5.6.2/usr/serec", [{outdir, "c:/Program Files/erl5.6.2/usr/"}]).
c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer/1 undefined
c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer2/1 undefined
c:/Program Files/erl5.6.2/usr/serec.erl:23: premature end
error
3>


obv this is just a toy program but say I have a function that squares the integers of a list from 1 to the send parameter.
so i want to send a message to that function.


-module(serec).
-export([seq/2,squarer/1,squarer2/1]).
         
seq(Start, End) -> seq(Start, End, []).

seq(Start, End, Acc) when Start =< End ->
   seq(Start, End-1, [End|Acc]);
seq(_, _, Acc) ->
   Acc.

squarer(X) ->
    receive
    Pattern [when Pattern > 7] ->
        [X*X || X <- [seq(1, 7]];
    end

squarer2(X) ->
    receive
    when X > 7 -> [X*X || X <- [seq(1, 7]];
    end

squarer(X) ! 2+6

 

Låna pengar utan säkerhet.
Sök och jämför lån hos Kelkoo.

<div>

<div class="Section1">

<p class="MsoNormal"><span>You don&rsquo;t send messages to
functions, you send them to processes.&nbsp; Also, you&rsquo;re missing periods after the squarer
and squarer2 functions (after the ends), which accounts for two of the compiler
errors.</span></p>

<p class="MsoNormal"><span>&nbsp;</span></p>

<div>

<div>

<div class="MsoNormal" align="center">

</div>

<p class="MsoNormal"><span>From:</span><span> erlang-questions-bounces <at> erlang.org
[mailto:erlang-questions-bounces <at> erlang.org] On Behalf Of not norwegian
swede<br>Sent: Thursday, July 03, 2008 04:04<br>To: erlang-questions <at> erlang.org<br>Subject: [erlang-questions] help with message-passing syntax</span></p>

</div>

<p class="MsoNormal">&nbsp;</p>

<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0"><tr>
<td valign="top">
  <p class="MsoNormal">im using:<br>
  http://www.erlang.org/doc/reference_manual/part_frame.html<br>
  but cant really figure it out. i didnt expect this to work but compiled
  anyway:<br>
  2&gt; c("c:/Program Files/erl5.6.2/usr/serec", [{outdir,
  "c:/Program Files/erl5.6.2/usr/"}]).<br>
  c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer/1 undefined<br>
  c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer2/1 undefined<br>
  c:/Program Files/erl5.6.2/usr/serec.erl:23: premature end<br>
  error<br>
  3&gt; <br><br><br>
  obv this is just a toy program but say I have a function that squares the
  integers of a list from 1 to the send parameter.<br>
  so i want to send a message to that function.<br><br><br>
  -module(serec).<br>
  -export([seq/2,squarer/1,squarer2/1]).<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>
  seq(Start, End) -&gt; seq(Start, End, []).<br><br>
  seq(Start, End, Acc) when Start =&lt; End -&gt;<br>
  &nbsp;&nbsp; seq(Start, End-1, [End|Acc]);<br>
  seq(_, _, Acc) -&gt;<br>
  &nbsp;&nbsp; Acc.<br><br>
  squarer(X) -&gt;<br>
  &nbsp;&nbsp;&nbsp; receive<br>
  &nbsp;&nbsp; &nbsp;Pattern [when Pattern &gt; 7] -&gt;<br>
  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; [X*X || X &lt;- [seq(1, 7]];<br>
  &nbsp;&nbsp;&nbsp; end<br><br>
  squarer2(X) -&gt;<br>
  &nbsp;&nbsp;&nbsp; receive<br>
  &nbsp;&nbsp; &nbsp;when X &gt; 7 -&gt; [X*X || X &lt;- [seq(1, 7]];<br>
  &nbsp;&nbsp;&nbsp; end<br><br>
  squarer(X) ! 2+6</p>
  </td>
 </tr></table>
<p class="MsoNormal">&nbsp;</p>

<div class="MsoNormal" align="center">

</div>

<p class="MsoNormal">L&aring;na pengar utan s&auml;kerhet.<br><a href="http://www.kelkoo.se/c-100390123-lan-utan-sakerhet.html?partnerId=96915014">S&ouml;k
och j&auml;mf&ouml;r l&aring;n hos Kelkoo.</a></p>

</div>

</div>

</div>
litao cheng | 4 Jul 03:55

Re: help with message-passing syntax

I'm sorry, you code has so many errors.
I think you'd better look at the book <programming erlang >, and the erlang referance manual.

the fllow code snips is my code, but I don't know If it's your need. I hope it's useful for you:
%%%%%%%%%%%%%%%%%%%%%%
-module(serec).
-export([start/0, get_squarer/2]).
        
seq(Start, End) -> seq(Start, End, []).

seq(Start, End, Acc) when Start =< End ->
   seq(Start, End-1, [End|Acc]);
seq(_, _, Acc) ->
   Acc.

squarer() ->
    receive
        {From, N} when N > 7 ->
        From ! {self(), [X*X || X <- seq(1, 7)]},
        squarer();
      {From, N} ->
          From ! {self(), [X*X || X <- seq(1, N)]},
          squarer();
      Other ->
          Other
    end.

%% <at> spec squarer() -> Pid
%% <at> doc create the squarer process
start() ->
    spawn(fun squarer/0).

%% <at> spec get_squarer(Pid, N) -> Result
%% <at> doc get the result form squarer process
get_squarer(Pid, N) when is_pid(Pid) ->
    Pid ! {self(), N},
    receive
        {Pid, Result} ->
            Result;
        Other ->
            Other
    end.
%%%%%%%%%%%%%%%%%%%

usage:
4> c(serec).
{ok,serec}
5> Pid = serec:start().
<0.49.0>
6> serec:get_squarer(Pid, 3).
[1,4,9]
7> serec:get_squarer(Pid, 4).
[1,4,9,16]
8> serec:get_squarer(Pid, 5).
[1,4,9,16,25]
9> serec:get_squarer(Pid, 10).
[1,4,9,16,25,36,49]


2008/7/3 not norwegian swede <notnorwegian <at> yahoo.se>:
im using:
http://www.erlang.org/doc/reference_manual/part_frame.html
but cant really figure it out. i didnt expect this to work but compiled anyway:
2> c("c:/Program Files/erl5.6.2/usr/serec", [{outdir, "c:/Program Files/erl5.6.2/usr/"}]).
c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer/1 undefined
c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer2/1 undefined
c:/Program Files/erl5.6.2/usr/serec.erl:23: premature end
error
3>


obv this is just a toy program but say I have a function that squares the integers of a list from 1 to the send parameter.
so i want to send a message to that function.


-module(serec).
-export([seq/2,squarer/1,squarer2/1]).
         
seq(Start, End) -> seq(Start, End, []).

seq(Start, End, Acc) when Start =< End ->
   seq(Start, End-1, [End|Acc]);
seq(_, _, Acc) ->
   Acc.

squarer(X) ->
    receive
    Pattern [when Pattern > 7] ->
        [X*X || X <- [seq(1, 7]];
    end

squarer2(X) ->
    receive
    when X > 7 -> [X*X || X <- [seq(1, 7]];
    end

squarer(X) ! 2+6


Låna pengar utan säkerhet.
Sök och jämför lån hos Kelkoo.
_______________________________________________
erlang-questions mailing list
erlang-questions <at> erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

<div>
<p>I'm sorry, you code has so many errors.<br>I think you'd better look at the book &lt;programming erlang &gt;, and the erlang referance manual.<br><br>the fllow code snips is my code, but I don't know If it's your need. I hope it's useful for you:<br>
%%%%%%%%%%%%%%%%%%%%%%<br>-module(serec).<br>-export([start/0, get_squarer/2]).<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>seq(Start, End) -&gt; seq(Start, End, []).<br><br>seq(Start, End, Acc) when Start =&lt; End -&gt;<br>&nbsp;&nbsp; seq(Start, End-1, [End|Acc]);<br>
seq(_, _, Acc) -&gt;<br>&nbsp;&nbsp; Acc.<br><br>squarer() -&gt;<br>&nbsp;&nbsp;&nbsp; receive<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {From, N} when N &gt; 7 -&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; From ! {self(), [X*X || X &lt;- seq(1, 7)]},<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; squarer();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {From, N} -&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; From ! {self(), [X*X || X &lt;- seq(1, N)]},<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; squarer();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Other -&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Other<br>&nbsp;&nbsp;&nbsp; end.<br><br>%%  <at> spec squarer() -&gt; Pid<br>%%  <at> doc create the squarer process<br>start() -&gt;<br>&nbsp;&nbsp;&nbsp; spawn(fun squarer/0).<br><br>%%  <at> spec get_squarer(Pid, N) -&gt; Result<br>
%%  <at> doc get the result form squarer process<br>get_squarer(Pid, N) when is_pid(Pid) -&gt;<br>&nbsp;&nbsp;&nbsp; Pid ! {self(), N},<br>&nbsp;&nbsp;&nbsp; receive<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {Pid, Result} -&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Result;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Other -&gt;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Other<br>
&nbsp;&nbsp;&nbsp; end.<br>%%%%%%%%%%%%%%%%%%%<br><br>usage:<br>4&gt; c(serec).<br>{ok,serec}<br>5&gt; Pid = serec:start().<br>&lt;0.49.0&gt;<br>6&gt; serec:get_squarer(Pid, 3).<br>[1,4,9]<br>7&gt; serec:get_squarer(Pid, 4).<br>[1,4,9,16]<br>
8&gt; serec:get_squarer(Pid, 5).<br>[1,4,9,16,25]<br>9&gt; serec:get_squarer(Pid, 10).<br>[1,4,9,16,25,36,49]<br><br><br></p>
<div class="gmail_quote">2008/7/3 not norwegian swede &lt;<a href="mailto:notnorwegian <at> yahoo.se">notnorwegian <at> yahoo.se</a>&gt;:<br><blockquote class="gmail_quote">
<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top">
im using:<br><a href="http://www.erlang.org/doc/reference_manual/part_frame.html" target="_blank">http://www.erlang.org/doc/reference_manual/part_frame.html</a><br>but cant really figure it out. i didnt expect this to work but compiled anyway:<br>
2&gt; c("c:/Program Files/erl5.6.2/usr/serec", [{outdir, "c:/Program Files/erl5.6.2/usr/"}]).<br>c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer/1 undefined<br>c:/Program Files/erl5.6.2/usr/serec.erl:2: function squarer2/1 undefined<br>
c:/Program Files/erl5.6.2/usr/serec.erl:23: premature end<br>error<br>3&gt; <br><br><br>obv this is just a toy program but say I have a function that squares the integers of a list from 1 to the send parameter.<br>so i want to send a message to that function.<br><br><br>-module(serec).<br>-export([seq/2,squarer/1,squarer2/1]).<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>seq(Start, End) -&gt; seq(Start, End, []).<br><br>seq(Start, End, Acc) when Start
 =&lt; End -&gt;<br>&nbsp;&nbsp; seq(Start, End-1, [End|Acc]);<br>seq(_, _, Acc) -&gt;<br>&nbsp;&nbsp; Acc.<br><br>squarer(X) -&gt;<br>&nbsp;&nbsp;&nbsp; receive<br>&nbsp;&nbsp; &nbsp;Pattern [when Pattern &gt; 7] -&gt;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; [X*X || X &lt;- [seq(1, 7]];<br>&nbsp;&nbsp;&nbsp; end<br><br>squarer2(X) -&gt;<br>&nbsp;&nbsp;&nbsp; receive<br>&nbsp;&nbsp; &nbsp;when X &gt; 7 -&gt; [X*X || X &lt;- [seq(1, 7]];<br>&nbsp;&nbsp;&nbsp; end<br><br>squarer(X) ! 2+6<br><br>
</td></tr></table>
<br>
L&aring;na pengar utan s&auml;kerhet.<br><a href="http://www.kelkoo.se/c-100390123-lan-utan-sakerhet.html?partnerId=96915014" target="_blank">S&ouml;k och j&auml;mf&ouml;r l&aring;n hos Kelkoo.</a><br>_______________________________________________<br>
erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> erlang.org</a><br><a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote>
</div>
<br>
</div>
Richard A. O'Keefe | 4 Jul 04:15
Favicon

Re: help with message-passing syntax


On 3 Jul 2008, at 9:04 pm, not norwegian swede wrote:
> squarer(X) ->
>     receive
>     Pattern [when Pattern > 7] ->
>         [X*X || X <- [seq(1, 7]];
>     end

Why the square brackets around the guard?

You are receiving Pattern, and checking it,
but then you do nothing with it!
Pattern is a bad name because it tells us nothing
whatever about the kind of messages you are expecting
to receive.

You have an argument X, and then you appear to be
trying to bind a *different* X in the list comprehension.

Your list comprehension

	[X*X || X <- [seq(1, 7)]]

has an extra pair of square brackets, for no apparent
reason.  The list [seq(1, 7)]] has only one element,
so X will be bound (just once) to [1,2,3,4,5,6,7].
However, [1,2,3,4,5,6,7]*[1,2,3,4,5,6,7] is not understood.
Presumably you meant

	[X*X || X <- seq(1, 7)]

However, having computed the list [1,4,9,...,49], what do
you want to do with it?

I would expect a squaring process to be something like

	squarer(Destination) ->
	    receive Number ->
		Destination ! (Number * Number),
		squarer(Destination)
	    end.

> squarer2(X) ->
>     receive
>     when X > 7 -> [X*X || X <- [seq(1, 7]];
>     end

This has two of the same defects as the previous version:
X used as argument and as list element pattern,
extra brackets around seq(1, 7).
It adds a new one: there is no pattern in your 'receive'
saying _what_ you want to receive or letting you remember
what it is.
>
>
> squarer(X) ! 2+6

This will call the *FUNCTION* squarer, in the expectation
that it will return a process ID.  The function squarer()
will enter the 'receive', and wait forever for a message.

I think you mean something like this:

	destination() ->
	    receive
		stop ->
		    ok
	      ; Number ->
		    io:write(Number), io:nl(),
		    destination()
	    end.

	squarer(Destination) ->
	    receive
		stop ->
		    Destination ! stop
	      ; Number ->
		    Destination ! (Number * Number),
		    squarer(Destination)
	    end.

	sender(Squarer, Numbers) ->
	    [Squarer ! Number || Number <- Numbers],
	    Squarer ! stop.

	example() ->
	    Destination = spawn(fun () -> destination() end),
	    Squarer = spawn(fun () -> squarer(Destination) end),
	    sender(Squarer, seq(1, 7)).

(Tested.)
>


Gmane