-module(counter). -export([start/0, count/1, next/0, set/1, stop/0]). start() -> register(counterPid, spawn(?MODULE, count, [0])). count(N) when N > 32767 -> count(0); count(N) -> receive {next, FromPID} -> FromPID ! {next, N}, count(N+1); {set, New} -> count(New); {stop} -> exit(normal) end. set(N) -> counterPid ! {set, N}. next() -> counterPid ! {next, self()}, receive {next, N} -> N end. stop() -> counterPid ! {stop}.