Erlang

From G1Joshi/Agent-Skills · View interactive version

Erlang is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Erlang programming assistance covering BEAM virtual machine, actor concurrency, supervision trees, and OTP. Use when developing telecom systems, distributed databases, or messaging backbones.

How to use this skill

Skill file

Erlang

Erlang is a battle-tested functional programming language designed for massive concurrency, high fault-tolerance, and distributed telecom systems on the BEAM virtual machine.

When to Use

Quick Start

-module(hello).
-export([start/0]).

start() ->
    io:format("Hello from Erlang/OTP!~n").

Core Concepts

Open Telecom Platform (OTP) GenServer Behavior

Standardized generic client-server framework managing state, calls, and asynchronous casts:

-module(counter_server).
-behaviour(gen_server).

-export([start_link/0, increment/0, get_count/0]).
-export([init/1, handle_call/3, handle_cast/2, terminate/2]).

start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
increment()  -> gen_server:cast(?MODULE, inc).
get_count()  -> gen_server:call(?MODULE, get).

init([]) -> {ok, 0}.

handle_call(get, _From, Count) -> {reply, Count, Count}.
handle_cast(inc, Count)        -> {noreply, Count + 1}.
terminate(_Reason, _State)    -> ok.

Pattern Matching Function Clauses

Functions declare multiple clauses matching input parameters structurally:

-module(finance).
-export([tax/2]).

tax(Amount, {state, "CA"}) -> Amount * 0.0825;
tax(Amount, {state, "NY"}) -> Amount * 0.08875;
tax(Amount, {state, _Other}) -> Amount * 0.05.

Hot Code Upgrades

Reloads module code live in memory while preserving active process state:

% Compile and reload module in running Erlang VM
c(my_worker).

Common Patterns

Simple Actor Message Passing Loop

Problem: Managing asynchronous message queues and state without threads and mutexes.

Solution:

Use tail-recursive receive loops:

-module(actor_counter).
-export([loop/1, start/0]).

start() ->
    spawn(fun() -> loop(0) end).

loop(Count) ->
    receive
        {increment, N} ->
            loop(Count + N);
        {get, Sender} ->
            Sender ! {current_count, Count},
            loop(Count);
        stop ->
            ok
    end.

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------------------- | :------------------------------------------------------------------ | :-------------------------------------------------------------------------------- |

| {badmatch, Value} | Pattern matching failed on = or function argument. | Verify return structure and provide clauses for all expected outcomes. |

| noproc: process not alive | Sending a message or RPC call to a process PID that already exited. | Verify supervisor restart strategies and link/monitor processes with monitor/2. |

| function_clause: no function clause matching | Invoking function with arguments that don't match any head clause. | Check argument types, arity, and guard conditions. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.