Picat List Comprehensions in Picat 2 — Questions and Answers
Question 1: What is the length of the list produced by [X : X in 1..10, X mod 3 =:= 0] in Picat?
- 3 (Correct answer)
- 4
- 33
- 10
Correct answer: 3
Multiples of 3 from 1 to 10 are 3, 6, and 9 — exactly 3 values — so len([X : X in 1..10, X mod 3 =:= 0]) equals 3.
Question 2: Can the result of a Picat list comprehension be used as the source list in another comprehension?
- No, comprehensions cannot be nested and cause a compile error
- Yes, a comprehension evaluates to a plain list and can serve as the generator in an outer comprehension (Correct answer)
- Only if the inner comprehension uses a numeric range generator
- Only when tabling mode is enabled for the predicate
Correct answer: Yes, a comprehension evaluates to a plain list and can serve as the generator in an outer comprehension
A Picat list comprehension evaluates eagerly to an ordinary list, so it can appear wherever a list is expected, including as the generator source in another comprehension.
Question 3: What does [X*2 : X in [1,2,3], X > 1] produce in Picat?
- [2, 4, 6]
- [4, 6] (Correct answer)
- [1, 4, 6]
- [2, 6]
Correct answer: [4, 6]
The condition X > 1 excludes X=1, so only X=2 (producing 4) and X=3 (producing 6) pass the filter, giving [4, 6].
Question 4: Which Picat expression filters the list [3,7,2,8,1,6] to keep only elements greater than 5?
- select(>5, [3,7,2,8,1,6])
- [X : X in [3,7,2,8,1,6], X > 5] (Correct answer)
- [X in [3,7,2,8,1,6] : X > 5]
- filter([3,7,2,8,1,6], X > 5)
Correct answer: [X : X in [3,7,2,8,1,6], X > 5]
The list comprehension [X : X in [3,7,2,8,1,6], X > 5] iterates the list and retains only 7, 8, and 6, which satisfy X > 5.
Question 5: Which of the following demonstrates using a Picat list comprehension with a built-in aggregate operation?
- sum([X : X in 1..100, X mod 2 =:= 0]) computes the sum of all even numbers from 1 to 100 (Correct answer)
- [sum : X in 1..100, X mod 2 =:= 0] computes a running total across iterations
- collect(sum, [X : X in 1..100]) is the standard Picat idiom for summation
- aggregate([X : X in 1..100], sum, even) is the proper form for filtering and summing
Correct answer: sum([X : X in 1..100, X mod 2 =:= 0]) computes the sum of all even numbers from 1 to 100
sum/1 accepts any list, so sum([X : X in 1..100, X mod 2 =:= 0]) builds the list of even numbers then sums them, equaling 2550.
Question 6: Which Picat list comprehension generates all pairs (X,Y) where X and Y are both from [1,2,3] and X < Y?
- [(X,Y) : X in [1,2,3], Y in [1,2,3], X < Y] (Correct answer)
- pairs([1,2,3], [1,2,3])
- [X-Y : X,Y in [1,2,3] : X < Y]
- combination([1,2,3], 2)
Correct answer: [(X,Y) : X in [1,2,3], Y in [1,2,3], X < Y]
Two generators over the same list with condition X < Y produce all strictly ordered pairs: (1,2), (1,3), and (2,3) as Picat tuple terms.
Question 7: What does sort([X : X in [3,1,4,1,5,9,2,6]]) produce in Picat?
- [1, 1, 2, 3, 4, 5, 6, 9]
- [1, 2, 3, 4, 5, 6, 9] (Correct answer)
- [9, 6, 5, 4, 3, 2, 1]
- [3, 1, 4, 1, 5, 9, 2, 6]
Correct answer: [1, 2, 3, 4, 5, 6, 9]
Picat's sort/1, like Prolog's, sorts the list in ascending order and removes duplicates, so the repeated 1 appears only once in the result [1,2,3,4,5,6,9].
What is the length of the list produced by [X : X in 1..10, X mod 3 =:= 0] in Picat?