← All Picat Flashcard Decks

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
  1. What is the length of the list produced by [X : X in 1..10, X mod 3 =:= 0] in Picat?

    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.

  2. Can the result of a Picat list comprehension be used as the source list in another comprehension?

    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.

  3. What does [X*2 : X in [1,2,3], X > 1] produce in Picat?

    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].

  4. Which Picat expression filters the list [3,7,2,8,1,6] to keep only elements greater than 5?

    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.

  5. Which of the following demonstrates using a Picat list comprehension with a built-in aggregate operation?

    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.

  6. Which Picat list comprehension generates all pairs (X,Y) where X and Y are both from [1,2,3] and X < Y?

    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.

  7. What does sort([X : X in [3,1,4,1,5,9,2,6]]) produce in Picat?

    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].