0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · Lección

Coincidencia de patrones y guards

Domine la coincidencia de patrones y las cláusulas guard de Erlang, fundamentos de un código Erlang expresivo y sin bifurcaciones.

Coincidencia de patrones y guards es una lección gratuita de Erlang OTP: Distributed & Fault-Tolerant Systems Programming en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en 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 42

Destructuring 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 100

Matching 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 5

The Underscore Wildcard

The underscore wildcard _ matches anything and binds nothing — use it for parts you do not care about.

{_, Important} = {ignore_me, 99}.
% Important = 99

Function 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.

Preguntas frecuentes

¿La lección «Coincidencia de patrones y guards» es gratis?

Sí — el texto completo de «Coincidencia de patrones y guards» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming, actualiza a CoddyKit PRO. El curso de Erlang OTP: Distributed & Fault-Tolerant Systems Programming incluye 4 lecciones en total.

¿Qué aprenderé en «Coincidencia de patrones y guards»?

Domine la coincidencia de patrones y las cláusulas guard de Erlang, fundamentos de un código Erlang expresivo y sin bifurcaciones. Practicas Erlang OTP: Distributed & Fault-Tolerant Systems Programming con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

No se requiere experiencia previa. Erlang OTP: Distributed & Fault-Tolerant Systems Programming en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Coincidencia de patrones y guards»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Erlang OTP: Distributed & Fault-Tolerant Systems Programming?

Sí. Cada lección de Erlang OTP: Distributed & Fault-Tolerant Systems Programming incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Introducción a Erlang y la VM
  2. Procesos y mensajería en Erlang
  3. Patrones básicos de concurrencia
  4. Coincidencia de patrones y guards
← Volver a Erlang OTP: Distributed & Fault-Tolerant Systems Programming