Picat List Comprehensions in Picat 1 — Questions and Answers
Question 1: What is the correct Picat syntax for a list comprehension that collects all elements X from list L where X > 0?
- {X : X in L, X > 0}
- [X : X in L | X > 0]
- [X : X in L, X > 0] (Correct answer)
- filter(fun(X) => X > 0 end, L)
Correct answer: [X : X in L, X > 0]
Picat list comprehensions use the form [Expr : Var in List, Condition], where conditions after the comma act as filters on the generated elements.
Question 2: Which Picat expression generates the list [1, 4, 9, 16, 25]?
- [X*X : X in 1..5] (Correct answer)
- [X^2 | X <- [1,2,3,4,5]]
- map(X*X, 1..5)
- squares(5)
Correct answer: [X*X : X in 1..5]
In Picat, [X*X : X in 1..5] uses a range generator to iterate X from 1 to 5 inclusive and computes X*X for each value, yielding [1,4,9,16,25].
Question 3: What does [X : X in [1,2,3,4,5], X mod 2 =:= 0] evaluate to in Picat?
- [1, 3, 5]
- [2, 4] (Correct answer)
- [false, true, false, true, false]
- [1, 2, 3, 4, 5]
Correct answer: [2, 4]
The condition X mod 2 =:= 0 selects only even numbers; from [1,2,3,4,5] only 2 and 4 are even, so the result is [2, 4].
Question 4: In a Picat list comprehension [Expr : Var in Generator, Condition], what role does the Condition play?
- It specifies the output type of the resulting list
- It determines the step size when iterating over a numeric range
- It is a goal that must succeed for the current element to be included in the result (Correct answer)
- It defines the initial accumulator value for the iteration
Correct answer: It is a goal that must succeed for the current element to be included in the result
The condition is a goal (a predicate call or comparison) that must succeed for each candidate element; only elements for which it holds are included in the output list.
Question 5: What is the output of [X : X in 1..5] in Picat?
- [1, 2, 3, 4, 5] (Correct answer)
- [0, 1, 2, 3, 4]
- 1..5 (an unevaluated range term)
- A lazy sequence that evaluates elements on demand
Correct answer: [1, 2, 3, 4, 5]
The range 1..5 in a Picat list comprehension generates integers 1 through 5 inclusive, so [X : X in 1..5] produces the fully evaluated list [1,2,3,4,5].
Question 6: How does Picat handle multiple generators in [X+Y : X in [1,2], Y in [10,20]]?
- It computes only the diagonal pairs: (1,10) and (2,20), giving [11, 22]
- It computes the Cartesian product, yielding [11, 21, 12, 22] (Correct answer)
- It raises an error because multiple generators are not supported
- It interleaves the lists alternately: [11, 12, 21, 22]
Correct answer: It computes the Cartesian product, yielding [11, 21, 12, 22]
Multiple generators in Picat form a Cartesian product with the leftmost generator varying slowest: X=1 pairs with Y=10 and Y=20, then X=2 pairs with Y=10 and Y=20, yielding [11,21,12,22].
Question 7: Which of the following correctly computes the sum of squares of even numbers from 1 to 10 in Picat?
- sum_of(X*X, X in 1..10, X mod 2 =:= 0)
- sum([X*X : X in 1..10, X mod 2 =:= 0]) (Correct answer)
- reduce(+, [X*X : X in 1..10, X rem 2 == 0])
- aggregate_sum(X*X, member(X, 1..10), X mod 2 =:= 0)
Correct answer: sum([X*X : X in 1..10, X mod 2 =:= 0])
Wrapping a list comprehension in sum/1 first builds the list of even squares (4,16,36,64,100) and then sums them, giving 220.
What is the correct Picat syntax for a list comprehension that collects all elements X from list L where X > 0?