Correspondência de Padrões e Guardas
Domine a correspondência de padrões e as cláusulas de guarda do Erlang, a base de um código Erlang expressivo e sem ramificações.
Correspondência de Padrões e Guardas é uma aula grátis de Erlang OTP: Distributed & Fault-Tolerant Systems Programming no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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.
Perguntas Frequentes
A aula “Correspondência de Padrões e Guardas” é grátis?
Sim — o texto completo de “Correspondência de Padrões e Guardas” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, atualize para CoddyKit PRO. O curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming inclui 4 aulas no total.
O que vou aprender em “Correspondência de Padrões e Guardas”?
Domine a correspondência de padrões e as cláusulas de guarda do Erlang, a base de um código Erlang expressivo e sem ramificações. Você pratica Erlang OTP: Distributed & Fault-Tolerant Systems Programming com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Nenhuma experiência prévia é necessária. Erlang OTP: Distributed & Fault-Tolerant Systems Programming no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Correspondência de Padrões e Guardas”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Sim. Cada aula de Erlang OTP: Distributed & Fault-Tolerant Systems Programming inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Introdução a Erlang e à VM
- Processos e troca de mensagens em Erlang
- Padrões básicos de concorrência
- Correspondência de Padrões e Guardas