Using Standard Modules 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 Using Standard Modules flashcards as text
A developer needs to get a list of all files and directories in the current working directory. Which predicate from the `os` module should be used for this purpose?
Answer: os:listdir(".", List)
The `os:listdir(Path, List)` predicate is used to retrieve the names of entries in a directory. `Path` is the directory to scan, and `List` will be bound to a list of strings representing the files and subdirectories. Using `.` for the path refers to the current directory.
When processing a list of numerical data, you need to find the sum of all elements. Which function from the `math` module accomplishes this?
Answer: math:sum/1
The `math:sum/1` function takes a list of numbers as an argument and returns their sum. It is the standard function for this common mathematical operation.
A Picat script needs to be executed from the command line with several arguments. How can the script access these arguments?
Answer: By calling `sys:get_args()`
The `sys` module provides interaction with the Picat system environment. The `sys:get_args()` function returns a list of strings, where each string is a command-line argument passed to the script. [21, 24]
You need to combine two lists, `L1` and `L2`, into a single list of pairs, where each pair contains one element from `L1` and one from `L2` at the corresponding position. Which predicate from the `util` module is designed for this?
Answer: util:zip(L1, L2, Pairs)
The `util:zip/3` predicate is the standard way to combine two lists into a list of pairs. For example, `zip([a,b], [1,2], Z)` will result in `Z = [{a,1}, {b,2}]`. The name comes from the analogy of a zipper interlocking two sides.
A developer needs to read the entire content of a text file named `config.txt` into a single string. Which function from the `io` module should be used?
Answer: io:read_file_to_string("config.txt")
The `io:read_file_to_string/1` function is specifically designed to open, read the entire contents of a file, and return those contents as a single string, closing the file automatically.
To execute an external command-line program (e.g., `ls -l` or `dir`) from within a Picat script and capture its output, which predicate is most suitable?
Answer: os:shell(Command, Output)
The `os:shell/2` predicate is used to execute a shell command. The first argument is the command to run as a string, and the second argument is a variable that will be bound to the standard output of that command. [11]