Picat Data Structures and Types Questions and Answers — Questions and Answers
Question 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?
- M = [101:"A"], Grade = M[101]
- M = student(101, "A"), Grade = M.2
- import util. M = new_map(), M.put(101, "A"), Grade = M.get(101) (Correct answer)
- M = new_map(), M[101] := "A", Grade = M[101]
Correct 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.
Question 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]`?
- [H | T] = L (Correct answer)
- L = H :: T
- [H, T] = L
- H = L[1], T = L[2..4]
Correct 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.
Question 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?
- A list like `[Name, YearOfBirth, ChildrenList]`
- An array
- A map where keys are `"name"`, `"yob"`, and `"children"`
- A structure like `$person(Name, YearOfBirth, ChildrenList)` (Correct answer)
Correct 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.
Question 4: Which of the following statements correctly identifies the data type of the Picat term `'hello world'`?
- It is a string, which is a list of characters.
- It is an atom. (Correct answer)
- It is a variable.
- It is a list containing two atoms, `hello` and `world`.
Correct 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.
Question 5: What is the result of the unification `$point(X, Y) = $point(10, 20)` in Picat?
- The unification fails because structures cannot be unified directly.
- A syntax error occurs.
- X is unified with `$point(10, 20)` and Y remains unbound.
- X is unified with `10`, and Y is unified with `20`. (Correct answer)
Correct 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`.
Question 6: What is the fundamental difference between `L1 = ["a", "b"]` and `L2 = [a, b]` in Picat?
- There is no difference; they are syntactically equivalent.
- `L1` is a valid list, but `L2` will cause a syntax error.
- `L1` is a list of variables, while `L2` is a list of atoms.
- `L1` is a list of strings, while `L2` is a list of atoms. (Correct answer)
Correct 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`.
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?