ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Erlang GenServer State Transition Logic

Erlang

Goal -- WPM

Ready
Exercise Algorithm Area
1-module(counter_server).
2
3-export([start_link/0, stop/0, increment/1, get/1, reset/1]).
4
5-behavior(gen_server).
6
7%% API functions
8start_link() ->
9gen_server:start_link({local, counter}, ?MODULE, [], []).
10
11stop() ->
12gen_server:stop(counter).
13
14increment(Pid) ->
15gen_server:call(Pid, increment).
16
17get(Pid) ->
18gen_server:call(Pid, get).
19
20reset(Pid) ->
21gen_server:call(Pid, reset).
22
23%% gen_server callbacks
24init([]) ->
25{ok, 0}.
26
27handle_call(increment, _From, State) ->
28NewState = State + 1,
29{reply, ok, NewState};
30
31handle_call(get, _From, State) ->
32{reply, State, State};
33
34handle_call(reset, _From, _State) ->
35{reply, ok, 0};
36
37handle_call(Other, _From, State) ->
38%% Handle unexpected calls gracefully
39io:format("Received unexpected call: ~p~n", [Other]),
40{reply, {error, unknown_command}, State}.
41
42handle_cast(_Msg, State) ->
43%% No cast messages expected for this simple server
44{noreply, State}.
45
46handle_info(_Info, State) ->
47%% No async info messages expected
48{noreply, State}.
49
50terminate(_Reason, _State) ->
51ok.
52
53code_change(_OldVsn, State, _Extra) ->
54{ok, State}.
Algorithm description viewbox

Erlang GenServer State Transition Logic

Algorithm description:

This Erlang GenServer implements a simple counter. It handles synchronous `call` requests for incrementing, retrieving, and resetting the counter's value. The `init` function sets the initial state to 0. The `handle_call` function pattern matches on incoming messages to perform the correct state transition and return the appropriate reply. This pattern is fundamental for building stateful services in Erlang.

Algorithm explanation:

The `counter_server` module defines a GenServer that maintains an integer state representing a counter. The `init/1` callback initializes the state to 0. The `handle_call/3` callback is the core logic, pattern matching on the message received. For `increment`, it adds 1 to the state and replies with `ok` and the new state. For `get`, it replies with the current state without changing it. For `reset`, it sets the state back to 0 and replies with `ok`. Any other call results in an error reply. The `handle_cast/2`, `handle_info/2`, `terminate/2`, and `code_change/3` callbacks are included for completeness but are not actively used in this simple example. The time complexity for each operation is O(1) as it involves simple arithmetic and state updates. Space complexity is O(1) as it only stores the counter value.

Pseudocode:

Start GenServer with initial state 0.
Handle 'increment' call: increment state, reply 'ok' and new state.
Handle 'get' call: reply current state and current state.
Handle 'reset' call: set state to 0, reply 'ok' and new state (0).
Handle any other call: reply with an error.
Ignore cast and info messages.
Terminate gracefully.