โ† All Picat Flashcard Decks

Data Structures and Types Flashcards

6 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 6 Data Structures and Types flashcards as text
  1. A developer needs to store student IDs (integers) as keys and their corresponding grades (strings) as values, with frequent lookups. Which Picat code snippet correctly creates a map, adds a student's grade, and retrieves it?

    Answer: import util. M = new_map(), M.put(101, "A"), Grade = M.get(101)

    Picat's `util` module provides the `new_map/0` function to create a hash map. The map's methods, such as `put/3` and `get/2`, are used to add and retrieve key-value pairs. The other options use syntax for lists, structures, or incorrect assignment operators not applicable to maps.

  2. Given the Picat list `L = [a, b, c, d]`, which unification will successfully bind the variable `H` to the atom `a` and the variable `T` to the list `[b, c, d]`?

    Answer: [H | T] = L

    The `[Head | Tail]` syntax is the fundamental pattern for deconstructing a list in Picat. The `|` operator separates the first element (the head) from the rest of the list (the tail). The other options use incorrect syntax or operators for this specific type of list decomposition.

  3. You are designing a system to represent a simple family tree. You need to store a person's name, year of birth, and a list of their children (who are also people). Which data structure is most idiomatic and efficient for representing a single person with these fixed fields?

    Answer: A structure like `$person(Name, YearOfBirth, ChildrenList)`

    A structure (or compound term) is the most idiomatic and efficient choice in Picat for representing a fixed set of related data fields like a record. It provides a clear, named representation (`person`) and allows for efficient access to its components through unification. The dollar sign `$` is required to distinguish the structure from a function call.

  4. Which of the following statements correctly identifies the data type of the Picat term `'hello world'`?

    Answer: It is an atom.

    In Picat, any sequence of characters enclosed in single quotes is treated as a single atom. Strings, which are lists of characters, are enclosed in double quotes. Variables must begin with an uppercase letter or an underscore.

  5. What is the result of the unification `$point(X, Y) = $point(10, 20)` in Picat?

    Answer: X is unified with `10`, and Y is unified with `20`.

    Unification of structures succeeds if they have the same functor (name) and arity (number of arguments), and their corresponding arguments can be unified. Here, the functors are both `point` and the arity is 2, so the unification proceeds by unifying the arguments: `X` with `10` and `Y` with `20`.

  6. What is the fundamental difference between `L1 = ["a", "b"]` and `L2 = [a, b]` in Picat?

    Answer: `L1` is a list of strings, while `L2` is a list of atoms.

    In Picat, double quotes `""` create strings (which are lists of character codes), while unquoted, lowercase identifiers are atoms. Therefore, `L1` is a list containing two strings, `"a"` and `"b"`. `L2` is a list containing two atoms, `a` and `b`.