Picat Functions vs. Predicates 2 — Questions and Answers
Question 1: Which syntax correctly defines a Picat function that returns the square of X?
- square(X) => X * X
- square(X) = X * X (Correct answer)
- square(X) :- X * X
- square(X) -> X * X
Correct answer: square(X) = X * X
Picat functions are defined with `=` between the head and the return expression.
Question 2: In Picat, what happens when you call a function but no clause pattern matches the arguments?
- The function silently fails
- The function returns `none`
- An existence error or pattern-match failure is thrown (Correct answer)
- The function backtracks to the previous choice point
Correct answer: An existence error or pattern-match failure is thrown
If no function clause matches, Picat raises an exception because functions must return a value.
Question 3: A Picat predicate clause uses `?=>` instead of `=>`. What does this indicate?
- The predicate is a function in disguise
- The predicate is nondeterministic and may leave choice points (Correct answer)
- The predicate always fails on backtracking
- The predicate runs in a separate thread
Correct answer: The predicate is nondeterministic and may leave choice points
`?=>` marks a nondeterministic clause that keeps a choice point, allowing backtracking.
Question 4: Can a Picat function be embedded directly inside an arithmetic expression such as `Y = f(X) + 3`?
- No, functions must be called with `call/1`
- Yes, because functions return values usable in expressions (Correct answer)
- Only if `f` is declared as `arithmetic`
- Only inside `once/1` wrappers
Correct answer: Yes, because functions return values usable in expressions
Picat functions return values and can appear anywhere an expression is expected.
Question 5: Which of the following is a valid way to call a Picat predicate `greet(Name)`?
- X = greet("Alice")
- greet("Alice") (Correct answer)
- print(greet("Alice"))
- Z is greet("Alice")
Correct answer: greet("Alice")
Predicates are called as statements; they do not return values and cannot appear inside expressions.
Question 6: In Picat, the body of a function clause and the body of a predicate clause share which feature?
- Both must end with a `return` statement
- Both use `=>` to separate head from body
- Both can include goals, assignments, and conditionals (Correct answer)
- Both must be deterministic
Correct answer: Both can include goals, assignments, and conditionals
Both function and predicate bodies are sequences of goals and can use the same control constructs.
Question 7: What is the correct way to define a recursive Picat function `fact(N)` for the factorial of N?
- fact(0) => 1. fact(N) => N * fact(N-1).
- fact(0) = 1. fact(N) = N * fact(N-1). (Correct answer)
- fact(N) :- N =:= 0, 1 ; N * fact(N-1).
- fact(N) = (N == 0 -> 1 ; N * fact(N-1))
Correct answer: fact(0) = 1. fact(N) = N * fact(N-1).
Recursive Picat functions use `=` and pattern-match across multiple clauses.
Which syntax correctly defines a Picat function that returns the square of X?