Picat Pattern Matching Rules 4 — Questions and Answers
Question 1: In Picat, what is the purpose of writing multiple clauses for the same predicate with different patterns?
- To dispatch behavior based on the structure or value of arguments (Correct answer)
- To define overloaded arithmetic operators
- To create parallel execution branches
- To declare optional parameters
Correct 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.
Question 2: What does `[H|T] = [a, b, c]` produce in Picat?
- H = a, T = [b, c] (Correct answer)
- H = [a], T = [b, c]
- H = a, T = b
- A type error because `=` is not for lists
Correct 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]`.
Question 3: Which of the following is a valid Picat rule that only matches when argument N is positive?
- positive(N), N > 0 => write(N). (Correct answer)
- positive(N) => N > 0, write(N).
- positive(N) :- N > 0.
- positive(N) when N > 0 => write(N).
Correct 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.
Question 4: In Picat, what happens when you use `=` to match a pattern inside a rule body rather than in the head?
- It performs unification at that point in execution, failing if patterns don't match (Correct answer)
- It assigns a new value, overwriting the old binding
- It is equivalent to `==` for comparison only
- It always succeeds and returns true
Correct 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.
Question 5: If a Picat function clause has the head `f(0)` and a body, for which calls will this clause be tried?
- Only calls where the argument unifies with 0 (Correct answer)
- All calls to f regardless of argument
- Only calls where the argument is a positive integer
- Only calls at the top level
Correct 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.
Question 6: In Picat, what does `_` (underscore) guarantee about variable binding?
- It matches any value and is never bound, even if it appears multiple times (Correct answer)
- It binds to the first value it sees and reuses that binding
- It must match an uninstantiated variable
- It only matches atoms
Correct 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.
Question 7: Which pattern matches a non-empty list in Picat?
- [_|_] (Correct answer)
- []
- [_]
- list(_)
Correct answer: [_|_]
`[_|_]` matches any list with at least one element, binding both head and tail to anonymous variables.
In Picat, what is the purpose of writing multiple clauses for the same predicate with different patterns?