Picat Using Standard Modules 4 — Questions and Answers
Question 1: What does `maps:put(Map, Key, Value, NewMap)` do?
- Modifies Map in-place with Key→Value
- Creates NewMap as Map with Key→Value added or updated (Correct answer)
- Deletes Key from Map and stores the result in NewMap
- Fails if Key already exists in Map
Correct answer: Creates NewMap as Map with Key→Value added or updated
`maps:put/4` is a functional-style operation producing NewMap from Map with the Key→Value binding added or overwritten.
Question 2: In the Picat `lists` module, what does `flatten/2` do?
- Sorts a nested list
- Converts a nested list into a flat list (Correct answer)
- Removes duplicates from a list
- Reverses a list
Correct answer: Converts a nested list into a flat list
`flatten/2` recursively removes nesting from a list, producing a flat single-level list.
Question 3: Which predicate in Picat's `io` module reads a single character from standard input?
- read_char/1
- get_char/1 (Correct answer)
- char_read/1
- read/1
Correct answer: get_char/1
`get_char/1` reads the next character from the current input stream and unifies it with its argument.
Question 4: What is the result of `sets:intersection([1,2,3,4], [2,4,6])` in Picat?
- [1,3,6]
- [1,2,3,4,6]
- [2,4] (Correct answer)
- []
Correct answer: [2,4]
Set intersection returns only elements present in both lists, which are 2 and 4.
Question 5: Which `math` module function raises a number to a power in Picat?
- power/2
- pow/2 (Correct answer)
- exp/2
- ** operator only
Correct answer: pow/2
`pow/2` computes X to the power Y; the `**` infix operator also works for the same purpose.
Question 6: In Picat's `string` module, what does `string_to_atom/2` do?
- Converts an atom to a string
- Converts a string to an atom (Correct answer)
- Tests whether a string equals an atom
- Creates a string from a character code
Correct answer: Converts a string to an atom
`string_to_atom/2` takes a string as first argument and unifies the second argument with the corresponding atom.
Question 7: What module must be imported to use Picat's constraint programming (`#=/2`, `#</2`) operators?
- cp (Correct answer)
- constraint
- clpfd
- sat
Correct answer: cp
Picat's `cp` module provides finite-domain constraint operators like `#=`, `#<`, and `#>` along with `solve/1`.
What does `maps:put(Map, Key, Value, NewMap)` do?