Picat Data Structures and Types 4 — Questions and Answers
Question 1: Which operator decomposes a compound term into a list `[Functor | Args]` in Picat?
- decomp/3
- =.. (the univ operator) (Correct answer)
- struct_to_list/2
- term_to_list/2
Correct answer: =.. (the univ operator)
The `=..` (univ) operator decomposes `f(a,b)` into `[f, a, b]` and can also construct terms from such lists.
Question 2: What does `f(a, b, c) =.. L` bind L to in Picat?
- [a, b, c]
- f([a, b, c])
- [f, a, b, c] (Correct answer)
- (f, a, b, c)
Correct answer: [f, a, b, c]
The univ operator produces a list whose head is the functor and whose tail is the argument list: `[f, a, b, c]`.
Question 3: What does `arg(1, point(3, 4), X)` bind X to in Picat?
- point
- 3 (Correct answer)
- 4
- Error: arg uses 0-based indexing
Correct answer: 3
`arg/3` is 1-indexed in Picat: arg 1 of `point(3,4)` is `3`, the first argument.
Question 4: Which predicate extracts the functor name and arity from a compound term?
- term_info(T, F, A)
- decompose(T, F, A)
- struct(T, F, A)
- functor(T, F, A) (Correct answer)
Correct answer: functor(T, F, A)
`functor(T, F, A)` binds F to the principal functor of T and A to its arity.
Question 5: What does `compound(hello)` evaluate to in Picat?
- true
- false (Correct answer)
- Error
- Depends on how hello is declared
Correct answer: false
`compound/1` fails for atoms like `hello` — a compound term must have a functor and at least one argument.
Question 6: What does `copy_term(f(X, X), Copy)` produce in Picat?
- Copy = f(X, X) (same original variables)
- Copy = f(_A, _B) where _A and _B are distinct fresh variables
- Copy = f(_A, _A) with a single new shared variable (Correct answer)
- Error: cannot copy terms with variables
Correct answer: Copy = f(_A, _A) with a single new shared variable
`copy_term/2` creates a renamed copy: both positions of X become the same new variable _A, preserving sharing.
Question 7: What is the arity of the Picat term `foo(a)`?
- 0
- 1 (Correct answer)
- 2
- foo
Correct answer: 1
Arity is the number of arguments a compound term takes; `foo(a)` has exactly one argument, so its arity is 1.
Which operator decomposes a compound term into a list `[Functor | Args]` in Picat?