[erlang-questions] help with message-passing syntax

litao cheng litaocheng@REDACTED
Fri Jul 4 03:55:39 CEST 2008


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@REDACTED>:

> 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.<http://www.kelkoo.se/c-100390123-lan-utan-sakerhet.html?partnerId=96915014>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080704/c9ca8864/attachment.htm>


More information about the erlang-questions mailing list