[erlang-questions] Newbie problems with gen_fsm

Oliver Korpilla <>
Sun May 15 19:50:00 CEST 2016

Further observations:

request_ticket(Price) ->
  gen_fsm:start(?MODULE, Price, []).


This functions returns {ok, Pid} or {error, Reason}

so to fit with the rest of your program, it probably should look like this:

request_ticket(Price) ->
  {ok, Pid}  gen_fsm:start(?MODULE, Price, []),


Gesendet: Sonntag, 15. Mai 2016 um 19:33 Uhr
Von: "Oliver Korpilla" <>
Cc: "Erlang Users' List" <>
Betreff: Re: [erlang-questions] Newbie problems with gen_fsm

When I see this:

awaiting_payment({payment, Amount}, From, {Price, Paid}) when Amount >= (Price - Paid) ->
io:format("~p paid ~p", [From, Amount]),
Change = Amount + Paid - Price,
{next_state, valid_ticket, {change, Change}, {Price, Price}};

There are too many elements in the tuple returned.

| {next_state,NextStateName,NewStateData}
| {next_state,NextStateName,NewStateData,Timeout}
| {next_state,NextStateName,NewStateData,hibernate}

You are allowed to return these when selecting next_state as your return value. But you return a 4-tuple instead of a 3-tuple.

If one of these is a reply back to the original requestor, then you need:


Also, not 100% sure, but:

init(Price) ->
{ok, awaiting_payment, {Price, 0}}.

I think it has to be:

init([Price]) ->

because init/1 takes the start_link params as a list.

Sorry, no Erlang system under my fingertips to test. Hope this helps.


Gesendet: Sonntag, 15. Mai 2016 um 16:26 Uhr
Von: TA <>
An: "Erlang Users' List" <>
Betreff: [erlang-questions] Newbie problems with gen_fsm

I wrote some code for a parking meter to understand how gen_fsm works. There are two states (awaiting_payment, valid_ticket), two input messages (payment, cancel_ticket), and three output messages (unpaid, change, refund).
I'm getting the following error and don't understand what's wrong. Any pointers?
** exception exit: {{function_clause,[{gen,do_for_proc,
     in function  gen_fsm:sync_send_event/2 (gen_fsm.erl, line 223)
     in call from ticketing_fsm:demo1/0 (ticketing_fsm.erl, line 25)
Source: https://transfer.sh/M7n3b/ticketing-fsm.erl
Tobias_______________________________________________ erlang-questions mailing list  http://erlang.org/mailman/listinfo/erlang-questions[http://erlang.org/mailman/listinfo/erlang-questions][http://erlang.org/mailman/listinfo/erlang-questions[http://erlang.org/mailman/listinfo/erlang-questions]]
erlang-questions mailing list


More information about the erlang-questions mailing list