[erlang-questions] state machine question

Bogdan Andu bog495@REDACTED
Wed Dec 9 09:36:26 CET 2015


Hi,

You can use a counter in internal state.

-record(state, { counter=0 }).

Every time you receive a message counter++

Erlang pseudocode:

loop(#state{counter = Counter} = State)  ->
receive 
     1 ->
       if Counter == 0 ->
             call b;
       true ->
              call c
      end,
      loop(State#state{counter = Counter + 1})
end
end
Bogdan
On Thursday, July 30, 2015 at 11:36:37 AM UTC+3, Ravindra M wrote:
>
> Hi All,
>           I am new to Erlang(been programming in python and C).
>
> I have trouble in implementing following program,
> Lets say I have,
> 1) function a which receives message in infinite loop.
> 2) function a should call function b when first message is received.
> 3) call function c when subsequent messages called.
>
> Lets assume there is no difference between message, example your always 
> receive '1'.
>
> So first '1', will call function b and all subsequent '1' call function c 
> executed.
>
> Thanks,
> Ravi
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151209/883a0109/attachment.htm>


More information about the erlang-questions mailing list