List Comprehensions in Picat 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 List Comprehensions in Picat flashcards as text
What is the correct Picat syntax for a list comprehension that collects all elements X from list L where X > 0?
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.
Which Picat expression generates the list [1, 4, 9, 16, 25]?
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].
What does [X : X in [1,2,3,4,5], X mod 2 =:= 0] evaluate to in Picat?
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].
In a Picat list comprehension [Expr : Var in Generator, Condition], what role does the Condition play?
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.
What is the output of [X : X in 1..5] in Picat?
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].
How does Picat handle multiple generators in [X+Y : X in [1,2], Y in [10,20]]?
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].
Which of the following correctly computes the sum of squares of even numbers from 1 to 10 in Picat?
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.