Pattern matching e guard
Padroneggi il pattern matching e le clausole guard di Erlang, alla base di un codice Erlang espressivo e privo di ramificazioni.
Pattern matching e guard è una lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Erlang OTP: Distributed & Fault-Tolerant Systems Programming, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Matching, Not Assigning
In Erlang = is the match operator, not assignment: it binds left-side variables to right-side values, or fails if they cannot match.
X = 42.
% X is now bound to 42Destructuring Tuples
Pattern matching can destructure compound terms like tuples in a single step, pulling values straight out.
{ok, Value} = {ok, 100}.
% Value is bound to 100Matching Lists
The [Head | Tail] pattern splits a list into its first element and the remaining rest.
[First | Rest] = [1, 2, 3].
% First = 1, Rest = [2, 3]The Pin / Bound Match
Once a variable is bound, reusing it in a pattern matches against its value rather than rebinding it.
X = 5.
{X, Y} = {5, 10}. % matches, Y = 10
{X, Y} = {7, 10}. % fails: X is bound to 5The Underscore Wildcard
The underscore wildcard _ matches anything and binds nothing — use it for parts you do not care about.
{_, Important} = {ignore_me, 99}.
% Important = 99Function Clause Matching
Function clauses are tried top to bottom, choosing the first whose arguments match. This replaces many if/else chains.
describe(0) -> zero;
describe(1) -> one;
describe(_) -> many.What Are Guards?
Guards add extra conditions to a clause with when; the clause fires only if both the pattern and the guard succeed.
classify(N) when N > 0 -> positive;
classify(N) when N < 0 -> negative;
classify(_) -> zero.Allowed Guard Expressions
Guards must be side-effect-free — only comparisons, arithmetic, and type tests like is_integer or is_list are allowed.
kind(X) when is_integer(X) -> int;
kind(X) when is_list(X) -> list;
kind(_) -> other.Combining Guards
Combine guard tests with , for AND and ; for OR.
in_range(X) when X >= 1, X =< 10 -> yes;
in_range(_) -> no.Matching in case
The case expression matches a value against patterns (with optional guards) and returns the branch that fits.
case lists:keyfind(id, 1, List) of
{id, V} -> V;
false -> not_found
end.Why It Matters
Pattern matching and guards make Erlang declarative — you describe data shapes instead of branching, the same skill you will use for messages.
Quick Check
Test your matching knowledge.
Recap
Recap: = matches and binds, you destructure tuples and lists, and function clauses with guards replace manual branching.
Domande Frequenti
La lezione «Pattern matching e guard» è gratuita?
Sì — il testo completo di «Pattern matching e guard» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming, passa a CoddyKit PRO. Il corso Erlang OTP: Distributed & Fault-Tolerant Systems Programming include 4 lezioni in totale.
Cosa imparerò in «Pattern matching e guard»?
Padroneggi il pattern matching e le clausole guard di Erlang, alla base di un codice Erlang espressivo e privo di ramificazioni. Eserciti Erlang OTP: Distributed & Fault-Tolerant Systems Programming con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Non è richiesta alcuna esperienza precedente. Erlang OTP: Distributed & Fault-Tolerant Systems Programming su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Pattern matching e guard»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Sì. Ogni lezione Erlang OTP: Distributed & Fault-Tolerant Systems Programming include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Introduzione a Erlang e alla VM
- Processi e messaggistica in Erlang
- Pattern fondamentali di concorrenza
- Pattern matching e guard