Picat Using Standard Modules Questions and Answers — Questions and Answers
Question 1: 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?
- os:files(.)
- os:scandir(".", List)
- os:listdir(".", List) (Correct answer)
- os:list_files(List)
Correct 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.
Question 2: When processing a list of numerical data, you need to find the sum of all elements. Which function from the `math` module accomplishes this?
- math:total/1
- math:sum/1 (Correct answer)
- math:aggr/1
- math:add_all/1
Correct 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.
Question 3: A Picat script needs to be executed from the command line with several arguments. How can the script access these arguments?
- By calling `os:get_cmd_args()`
- By accessing the built-in `ARGS` list
- By calling `sys:get_args()` (Correct answer)
- By reading from the `io:stdin` stream
Correct 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]
Question 4: 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?
- util:join(L1, L2, Pairs)
- util:combine(L1, L2, Pairs)
- util:merge(L1, L2, Pairs)
- util:zip(L1, L2, Pairs) (Correct answer)
Correct 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.
Question 5: 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?
- io:read_file_to_string("config.txt") (Correct answer)
- io:get_file_contents("config.txt")
- io:read_all("config.txt")
- io:read_file("config.txt")
Correct 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.
Question 6: 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?
- sys:run(Command, Output)
- os:shell(Command, Output) (Correct answer)
- io:execute(Command, Output)
- os:cmd(Command, Output)
Correct 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]
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?