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 does the `?=>` operator signify in a rule head compared to `=>`?
Answer: `?=>` is non-deterministic (allows backtracking); `=>` is deterministic (commits on match)
Rules using `?=>` are non-deterministic and allow the system to backtrack and try other clauses, while `=>` commits to the first matching clause.
Which Picat pattern correctly matches a list with exactly two elements?
Answer: [X, Y]
`[X, Y]` matches a list of exactly two elements, binding X to the first and Y to the second.
In a Picat rule, a guard expression appears:
Answer: After the rule head, preceded by a comma, before the neck operator
A guard is a condition placed after the pattern head and before the `=>` or `?=>` neck, separated by a comma.
What happens when no pattern in a Picat function definition matches the given arguments?
Answer: A runtime exception is thrown
If no clause pattern matches the arguments, Picat throws an existence error or no-clause exception at runtime.
Which pattern in Picat uses an anonymous variable to ignore a list element?
Answer: [_, X | _]
The underscore `_` is Picat's anonymous variable that matches any value without binding it.
Consider `foo([H|T]) => write(H).` โ what does H bind to when called as `foo([1,2,3])`?
Answer: 1
In the head pattern `[H|T]`, H binds to the first element (head) of the list, which is 1.
In Picat pattern matching, what does matching against an integer literal in the head accomplish?
Answer: It restricts the rule to fire only when the argument equals that exact integer
A literal in a pattern head acts as an equality constraint, so the rule only fires if the argument unifies with that exact value.