Functions vs. Predicates 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 Functions vs. Predicates flashcards as text
What does Picat's `to_string/1` function do that a predicate could not directly accomplish?
Answer: It returns a string value usable inline in expressions like `print(to_string(42) ++ "!")`
As a function, `to_string/1` returns a value immediately composable with operators like `++`.
In Picat, which of the following correctly adds a guard to a predicate clause?
Answer: classify(X), X > 0 => println(positive)
In Picat, a guard is placed between the head and the `=>` neck using a comma: `Head, Guard => Body`.
Picat functions can be higher-order. Which call correctly applies function `F` to value `X` and uses the result?
Answer: Result = call(F, X)
In Picat, `call(F, X)` invokes a function reference `F` with argument `X` and returns the result.
What is the key reason you cannot use a predicate directly inside a list comprehension expression in Picat?
Answer: List comprehensions expect expressions that yield values, which only functions provide
List comprehension expressions like `[f(X) : X in L]` require `f` to return a value, so `f` must be a function.
A Picat module exports both `area/1` (function) and `print_area/1` (predicate). Which import style correctly handles both?
Answer: import geometry
`import ModuleName` imports all exported functions and predicates from the module.
Consider `abs(X) = X, X >= 0 => X` and `abs(X) = -X`. What design pattern does this demonstrate?
Answer: Guarded function clauses with a fallback
The first clause has a guard `X >= 0`; if it fails, the second clause serves as the fallback.
Which statement best describes how Picat handles a predicate that is called but immediately fails?
Answer: Control backtracks to the most recent choice point or the call fails
A failing predicate causes backtracking; if no choice point exists, the enclosing goal fails.