Picat Functions vs. Predicates 3 — Questions and Answers
Question 1: In Picat, which operator separates the head from the body in a deterministic predicate clause?
- =
- => (Correct answer)
- ?=>
- :-
Correct answer: =>
`=>` is the neck operator for a deterministic predicate clause in Picat.
Question 2: Consider `len([]) = 0` and `len([_|T]) = 1 + len(T)`. Why must `len` be a function rather than a predicate here?
- Because it uses list patterns
- Because it produces a return value used in an arithmetic expression (Correct answer)
- Because it is recursive
- Because it has two clauses
Correct answer: Because it produces a return value used in an arithmetic expression
The result of `len` is used directly in `1 + len(T)`, requiring it to be a function that yields a value.
Question 3: Which statement about Picat functions is FALSE?
- A function clause can have a guard condition
- A function can call other predicates in its body
- A function can be nondeterministic by using `?=` (Correct answer)
- A function always returns exactly one value per successful call
Correct answer: A function can be nondeterministic by using `?=`
Picat does not have a `?=` nondeterministic function neck; functions are deterministic constructs.
Question 4: A Picat predicate `max_list(List, Max)` binds `Max` to the largest element. How is this different from a function approach?
- The predicate cannot use recursion
- The predicate unifies the result via an extra output argument instead of returning it (Correct answer)
- The predicate uses `=` to return Max
- There is no difference in Picat
Correct answer: The predicate unifies the result via an extra output argument instead of returning it
Predicates communicate results through extra arguments bound by unification, not through a return value.
Question 5: Given `double(X) = 2 * X`, what does `Y = double(5) + double(3)` evaluate to?
- Y = 10
- Y = 16 (Correct answer)
- Y = 6
- A syntax error is raised
Correct answer: Y = 16
`double(5)` returns 10 and `double(3)` returns 6, so `Y = 16`.
Question 6: In Picat, a function clause with a guard looks like `f(X) = V, Guard => Body`. When is `Guard` evaluated?
- Before pattern matching the head
- After the head matches but before executing the body or committing to the return expression (Correct answer)
- Only after the body executes
- Guards are not allowed on function clauses
Correct answer: After the head matches but before executing the body or committing to the return expression
The guard is checked after the head pattern matches; if it fails, the next clause is tried.
Question 7: Which built-in feature of Picat allows a predicate to behave like a function when exactly one solution is needed?
- `once(Goal)` (Correct answer)
- `assert(Goal)`
- `forall(Goal)`
- `freeze(Goal)`
Correct answer: `once(Goal)`
`once/1` commits to the first solution of a nondeterministic predicate, discarding remaining choice points.
In Picat, which operator separates the head from the body in a deterministic predicate clause?