Erlang OTP: Distributed & Fault-Tolerant Systems Programming · บทเรียน

การจับคู่รูปแบบและเงื่อนไขกำกับ

ทำความเข้าใจการจับคู่รูปแบบและอนุประโยคเงื่อนไขกำกับของ Erlang ซึ่งเป็นรากฐานของโค้ด Erlang ที่สื่อความหมายได้ดีและไม่ต้องแตกแขนง

บทเรียน 4 จาก 413 ขั้นตอน

การจับคู่รูปแบบและเงื่อนไขกำกับ เป็นบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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.

เริ่มต้นได้ฟรี

เรียนรู้ Erlang ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การจับคู่รูปแบบและเงื่อนไขกำกับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจับคู่รูปแบบและเงื่อนไขกำกับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจับคู่รูปแบบและเงื่อนไขกำกับ”

ทำความเข้าใจการจับคู่รูปแบบและอนุประโยคเงื่อนไขกำกับของ Erlang ซึ่งเป็นรากฐานของโค้ด Erlang ที่สื่อความหมายได้ดีและไม่ต้องแตกแขนง คุณปฏิบัติ Erlang OTP: Distributed & Fault-Tolerant Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Erlang OTP: Distributed & Fault-Tolerant Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจับคู่รูปแบบและเงื่อนไขกำกับ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming นี้ได้ไหม

ได้ บทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนะนำ Erlang และ VM
  2. โพรเซสและการรับส่งข้อความใน Erlang
  3. รูปแบบพื้นฐานของการทำงานพร้อมกัน
  4. การจับคู่รูปแบบและเงื่อนไขกำกับ
← กลับไปที่ Erlang OTP: Distributed & Fault-Tolerant Systems Programming