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 Picat list comprehension that flattens [[1,2],[3,4],[5]] into [1,2,3,4,5]?
Answer: [X : L in [[1,2],[3,4],[5]], X in L]
Using two generators — L iterates over the outer list of sublists, then X iterates over each sublist L — naturally flattens the nested structure into a single list.
How do you generate the 7-times multiplication table (7×1 through 7×10) as a list using a Picat list comprehension?
Answer: [7*X : X in 1..10]
The comprehension [7*X : X in 1..10] iterates X over the range 1 to 10 and multiplies each by 7, producing [7,14,21,28,35,42,49,56,63,70].
What does [X : X in [a,b,c,d,e], X \= b, X \= d] produce in Picat?
Answer: [a, c, e]
The two inequality conditions X \= b and X \= d both use structural non-unification, excluding the atoms b and d and leaving [a, c, e].
Which expression correctly sums all elements in a list List using a list comprehension in Picat?
Answer: sum([X : X in List])
sum/1 accepts any list and returns its arithmetic total; [X : X in List] evaluates to a copy of the same list, so sum([X : X in List]) is equivalent to sum(List).
Given a user-defined predicate is_prime/1, which Picat list comprehension generates all prime numbers up to 20?
Answer: [X : X in 2..20, is_prime(X)]
The comprehension [X : X in 2..20, is_prime(X)] generates candidates from 2 to 20 and keeps only those for which is_prime(X) succeeds.
What is the relationship between [X : X in L, true] and [X : X in L] in Picat?
Answer: They are equivalent because true always succeeds and filters out nothing
The built-in goal true always succeeds unconditionally, so adding it as a comprehension condition has no filtering effect, making both expressions produce the same list.
How many elements does [X : X in 1..5, Y in 1..5, X =:= Y] produce in Picat?
Answer: 5 (one element for each pair where X equals Y)
For each value of X in 1..5, only the matching Y satisfies X =:= Y, so the five diagonal pairs (1,1),(2,2),(3,3),(4,4),(5,5) each contribute one X value, giving [1,2,3,4,5].