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
In Picat, what is the purpose of writing multiple clauses for the same predicate with different patterns?
Answer: To dispatch behavior based on the structure or value of arguments
Multiple clauses with different patterns implement structural dispatch — Picat picks the clause whose pattern unifies with the actual arguments.
What does `[H|T] = [a, b, c]` produce in Picat?
Answer: H = a, T = [b, c]
The unification `[H|T] = [a,b,c]` binds H to the head `a` and T to the tail `[b,c]`.
Which of the following is a valid Picat rule that only matches when argument N is positive?
Answer: positive(N), N > 0 => write(N).
In Picat rule syntax, the guard `N > 0` is placed after the head pattern and before the `=>` neck, separated by a comma.
In Picat, what happens when you use `=` to match a pattern inside a rule body rather than in the head?
Answer: It performs unification at that point in execution, failing if patterns don't match
The `=` operator in Picat's body is unification — it tries to make both sides identical and throws an error if it fails.
If a Picat function clause has the head `f(0)` and a body, for which calls will this clause be tried?
Answer: Only calls where the argument unifies with 0
The literal `0` in the head pattern means the clause is only attempted when the first argument can unify with (equals) 0.
In Picat, what does `_` (underscore) guarantee about variable binding?
Answer: It matches any value and is never bound, even if it appears multiple times
Each occurrence of `_` is a fresh anonymous variable that matches anything without creating a binding or linking occurrences.
Which pattern matches a non-empty list in Picat?
Answer: [_|_]
`[_|_]` matches any list with at least one element, binding both head and tail to anonymous variables.