help with message-passing syntax

View: New views
4 Messages — Rating Filter:   Alert me  

help with message-passing syntax

by not norwegian swede :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: help with message-passing syntax

by David Mercer-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

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@... [mailto:erlang-questions-bounces@...] On Behalf Of not norwegian swede
Sent: Thursday, July 03, 2008 04:04
To: erlang-questions@...
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.


_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: help with message-passing syntax

by litaocheng :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

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

%% @spec get_squarer(Pid, N) -> Result
%% @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@...>:
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@...
http://www.erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: help with message-passing syntax

by Richard O'Keefe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.)
>



_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions