← All Picat Flashcard Decks

Pattern Matching Rules Flashcards

7 cards from real Picat practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 7 Pattern Matching Rules flashcards as text
  1. What is the result of matching the pattern `f(X, X)` against the term `f(3, 4)` in Picat?

    Answer: Match fails because X cannot unify with both 3 and 4

    When the same variable appears twice in a pattern, both positions must unify to the same value; since 3 ≠ 4, the match fails.

  2. In Picat, which rule type is searched in order and allows multiple clauses to be tried on backtracking?

    Answer: Non-deterministic rules using `?=>`

    Non-deterministic rules (`?=>`) allow Picat to backtrack and try subsequent clauses when the current one fails.

  3. How do you write an as-pattern in Picat to bind the whole list while also matching its head?

    Answer: L@[H|_]

    The `@` syntax (e.g., `L@[H|_]`) creates an as-pattern, binding L to the full term while destructuring it simultaneously.

  4. In Picat, if two clauses have the same head pattern and neither has a guard, which one executes?

    Answer: The first textually defined clause

    Picat searches clauses top-to-bottom; the first matching clause wins (and with `=>` commits, preventing backtracking to the second).

  5. What does the pattern `{X, Y, Z}` match in Picat?

    Answer: A tuple with exactly three elements

    Curly braces in Picat denote tuples, so `{X, Y, Z}` matches a three-element tuple and binds each position.

  6. Which Picat construct allows pattern matching with conditions in a single expression rather than separate clauses?

    Answer: The `cond` expression with `=>` branches

    Picat's `cond` expression evaluates branches with pattern-like conditions using `=>`, enabling inline conditional dispatch.

  7. When a Picat predicate clause head contains a compound term like `node(Left, Val, Right)`, what does matching against `node(_, 5, _)` verify?

    Answer: That the second field of the node structure equals 5, ignoring the other fields

    Anonymous variables `_` match anything, so `node(_, 5, _)` succeeds only when the middle field unifies with 5.