0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

การจับคู่รูปแบบและลำดับการควบคุม

เชี่ยวชาญความสามารถด้านการจับคู่รูปแบบอันทรงพลังของ Elixir และโครงสร้างควบคุมสำคัญอย่าง `if`, `case` และ `cond`

การจับคู่รูปแบบและลำดับการควบคุม เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

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

Intro to Pattern Matching

Pattern matching is the heart of Elixir. Here = is a match operator, not assignment — it makes the left side equal the right.

Basic Variable Matching

The simplest match binds an unbound variable to a value. If it is already bound, Elixir instead checks that the two sides actually match.

defmodule BasicMatchDemo do
  def run do
    # 'a' is unbound, so it's assigned 10
    a = 10
    IO.puts "a is: #{a}"

    # 'b' is unbound, so it's assigned 20
    b = 20
    IO.puts "b is: #{b}"

    # This matches 'a' to the value of 'b' (20)
    # 'a' is re-bound to 20
    a = b
    IO.puts "After a = b, a is: #{a}"
  end
end

BasicMatchDemo.run()

Matching with Literals

Put a literal on the left and the match becomes an assertion: it succeeds only if both sides are equal, otherwise it raises. Handy for validation.

defmodule LiteralMatchDemo do
  def run do
    # This matches because 1 is equal to 1
    1 = 1
    IO.puts "1 = 1 matched successfully!"

    # This matches because 'hello' is equal to 'hello'
    "hello" = "hello"
    IO.puts "'hello' = 'hello' matched successfully!"

    # If you uncomment the line below, it will cause a MatchError
    # 1 = 2
    # IO.puts "This line would not be reached if 1 = 2 failed"
  end
end

LiteralMatchDemo.run()

Deconstructing Tuples

Matching shines on tuples — deconstruct them straight into variables, which is exactly how you unpack {:ok, value} results.

defmodule TupleMatchDemo do
  def run do
    # Match a tuple to extract its elements
    {:person, name, age} = {:person, "Alice", 30}
    IO.puts "Name: #{name}, Age: #{age}"

    # Match a common result tuple
    {:ok, data} = {:ok, "User fetched successfully"}
    IO.puts "Status: ok, Data: #{data}"

    # If you uncomment the line below, it will cause a MatchError
    # {:error, reason} = {:ok, "Success"}
  end
end

TupleMatchDemo.run()

Deconstructing Lists

Deconstruct lists with the [head | tail] pattern: head is the first element, tail the rest. Use _ for parts you do not care about.

defmodule ListMatchDemo do
  def run do
    list = [1, 2, 3, 4, 5]

    # Match head and tail
    [head | tail] = list
    IO.puts "Head: #{head}, Tail: #{inspect tail}"

    # Match specific elements
    [first, second, third | _rest] = list
    IO.puts "First: #{first}, Second: #{second}, Third: #{third}"

    # Match an empty list
    [] = []
    IO.puts "Empty list matched!"
  end
end

ListMatchDemo.run()

The Pin Operator (^)

The pin operator ^ says "do not rebind — match against this variable's current value" instead. Great for asserting equality.

defmodule PinOperatorDemo do
  def run do
    value_a = 10
    value_b = 10

    # This uses the current value of value_a (10) to match against value_b
    # It succeeds because 10 matches 10
    ^value_a = value_b
    IO.puts "Match successful: value_a = #{value_a}, value_b = #{value_b}"

    value_c = 5

    # If you uncomment the line below, it will cause a MatchError
    # because ^value_a (10) is not equal to value_c (5)
    # ^value_a = value_c
    # IO.puts "This line would not be reached if ^value_a = value_c failed"
  end
end

PinOperatorDemo.run()

Simple Conditionals: if/unless

For simple branches use if and its mirror unless, each with an optional else. The cleanest tool for one true/false condition.

defmodule IfUnlessDemo do
  def run do
    number = 7

    if number > 5 do
      IO.puts "Number is greater than 5"
    else
      IO.puts "Number is 5 or less"
    end

    unless rem(number, 2) == 0 do
      IO.puts "Number is odd"
    else
      IO.puts "Number is even"
    end
  end
end

IfUnlessDemo.run()

Handling Multiple Patterns with case

The case expression matches one value against several patterns and runs the first that fits — pattern matching as control flow.

defmodule CaseExpressionDemo do
  def run do
    status = {:ok, "Data loaded"}

    case status do
      {:ok, message} ->
        IO.puts "Success: #{message}"
      {:error, reason} ->
        IO.puts "Failed: #{reason}"
      _ ->
        IO.puts "Unknown status"
    end

    # Another example with a simple value
    grade = "B"
    case grade do
      "A" -> IO.puts "Excellent!"
      "B" -> IO.puts "Good job!"
      _ -> IO.puts "Keep trying!"
    end
  end
end

CaseExpressionDemo.run()

Multiple Conditions with cond

When you have many boolean checks (an if/else-if chain elsewhere), cond runs the first clause whose condition is true.

defmodule CondExpressionDemo do
  def run do
    score = 85

    cond do
      score >= 90 -> IO.puts "Grade: A"
      score >= 80 -> IO.puts "Grade: B"
      score >= 70 -> IO.puts "Grade: C"
      true -> IO.puts "Grade: D or lower" # The 'true' clause acts as a catch-all
    end

    temperature = 25
    cond do
      temperature > 30 -> IO.puts "It's hot!"
      temperature > 20 -> IO.puts "It's warm."
      true -> IO.puts "It's cool."
    end
  end
end

CondExpressionDemo.run()

Pattern Matching Challenge

Consider the following Elixir code snippet:

data = ["apple", {:fruit, "banana"}, "cherry"]
[first, {:fruit, second}, _] = data

What will be the final values of the variables first and second after this code executes?

Recap: Pattern Matching & Control

Recap: = matches, you deconstruct tuples and lists, ^ pins a value, and if/case/cond drive your control flow.

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

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

ใช่ — ข้อความเต็มของ “การจับคู่รูปแบบและลำดับการควบคุม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

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

เชี่ยวชาญความสามารถด้านการจับคู่รูปแบบอันทรงพลังของ Elixir และโครงสร้างควบคุมสำคัญอย่าง `if`, `case` และ `cond` คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การจับคู่รูปแบบและลำดับการควบคุม” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. บทนำสู่ภาษา Elixir
  2. ชนิดข้อมูลและตัวดำเนินการพื้นฐาน
  3. การจับคู่รูปแบบและลำดับการควบคุม
  4. สตริง ไบนารี และซิกิลใน Elixir
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development