Functions vs. Predicates Flashcards
6 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 6 Functions vs. Predicates flashcards as text
What is the primary characteristic that distinguishes a Picat function from a Picat predicate?
Answer: Functions must always succeed and return exactly one value, while predicates can fail, succeed, or provide multiple answers via backtracking.
In Picat, a function is a specialized form of a predicate. The key difference is that a function is deterministic; it must always succeed and yield a single return value. Predicates, on the other hand, are more general: they can succeed (once or multiple times through backtracking) or they can fail.
A developer needs to implement a feature that finds all possible paths between two nodes in a graph. Which Picat construct is more suitable for this scenario and why?
Answer: A predicate, because it can use backtracking to find and yield each path as a separate solution.
A predicate is the ideal choice for finding all possible paths because this task is inherently non-deterministic. A predicate can be defined with backtrackable rules (using `?=>`) to explore different branches of the graph. Each time a path is found, the predicate succeeds, and backtracking allows the search to continue for other potential paths.
Which of the following statements about Picat functions is FALSE?
Answer: A function can be defined with backtrackable rules (`?=>`) to provide multiple return values.
Functions in Picat are deterministic and must return exactly one value. They cannot be defined with backtrackable rules (`?=>`), as this operator is specifically for non-deterministic predicates that can yield multiple solutions through backtracking. Functions always succeed once or raise an exception if no rule matches.
Consider the following Picat code snippet: ```picat main => X = my_calc(5), println(X). my_calc(N) = R, N > 0 => R = N * 2. ``` What is the role of `my_calc/1` in this program?
Answer: It is a function that returns the value of `N * 2`.
The syntax `my_calc(N) = R` defines a function named `my_calc` with one input argument `N` and a return value `R`. The expression `R = N * 2` calculates the return value. The call `my_calc(5)` evaluates to 10, which is then assigned to `X`.
When is it more appropriate to use a predicate instead of a function in Picat?
Answer: When the primary purpose is to check if a certain condition is true or false without returning a specific value.
Predicates are ideal for relational logic, where the outcome is success or failure. If the goal is simply to verify a condition (e.g., checking for membership in a list, `member/2`), a predicate is the natural choice. It succeeds if the condition holds and fails otherwise. While a function could be forced to return a boolean, a predicate more directly expresses this logic.
How does Picat handle a call to a function for which no pattern-matching rule applies?
Answer: The call raises an exception and terminates the program.
Unlike predicates which can simply fail, a function in Picat is expected to always succeed with one answer. If a function is called with arguments that do not match any of its defining rules, it cannot produce a value. This situation is treated as an error, and Picat will raise an exception.
Written and edited by Nathan Beja, founder of Practice Test Geeks, with AI assistance. Exam facts are checked against the official sources linked below. Editorial policy
Sources & references
Exam facts on this page (fees, format, dates, passing scores) are checked against these official sources; the date is our last check. Editorial policy
- officialasvab.com — Test delivery, eligibility, registration deadline, retake policy, score release, time limit, score validity, exam information · verified Dec 2026