โ† All Picat Flashcard Decks

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
  1. In Picat, `map(F, List) = [F(X) : X in List]` defines a higher-order map. Why must `F` be a function reference rather than a predicate reference here?

    Answer: The list comprehension builds its output from return values, requiring `F` to return a value

    Each element `F(X)` must evaluate to a value for inclusion in the new list, so `F` must be a function.

  2. What distinguishes a Picat action rule (using `=>` with an event trigger) from a regular predicate clause?

    Answer: Action rules are triggered by domain events during constraint solving, not by direct calls

    Action rules fire automatically when a variable's domain changes during constraint propagation, unlike regular predicate clauses.

  3. Picat's tabling mechanism can be applied to predicates. What is the effect of `table` on a predicate `path/2`?

    Answer: It memoizes computed results to avoid redundant recomputation during search

    The `table` declaration memoizes solutions, enabling efficient dynamic programming over predicates.

  4. In Picat, which syntax is used to define a function with multiple return clauses including a default case?

    Answer: Write multiple `= Expr` clauses with pattern matching or guards, ending with a catch-all

    Multiple function clauses with patterns or guards are the idiomatic way to handle multiple cases.

  5. Given predicate `between(Lo, Hi, X)` that nondeterministically binds X to each integer in [Lo, Hi], how would you collect all solutions into a list?

    Answer: `findall(X, between(1, 5, X), L)`

    `findall/3` collects all solutions of a goal into a list, bridging nondeterministic predicates and list data.

  6. Why is it generally preferred to implement a pure computation (e.g., converting Celsius to Fahrenheit) as a Picat function rather than a predicate?

    Answer: A function returns the result directly, making call sites cleaner and enabling expression composition

    Pure computations benefit from function syntax because the result can be embedded in expressions without an extra output argument.

  7. What does it mean when Picat documentation says a predicate is `det` (deterministic)?

    Answer: The predicate always succeeds exactly once without leaving a choice point

    A `det` predicate succeeds exactly once and leaves no choice points, behaving predictably like a function call.