Appendix C
The util Module
On this page
The util module provides useful utility functions and predicates. This module is expected to be expanded in the future. This module must be imported before use.
C.1 Utilities on Terms
- find_first_of(, ) = : This function returns the first index at which the argument unifies with . If there is no argument that unifies with , then this function returns -1. must be either a list or a structure.
- find_last_of(, ) = : This function returns the last index at which the argument unifies with . If there is no argument that unifies with , then this function returns -1. must be either a list or a structure.
- replace(, , ) = : This function returns a copy of , replacing all of the occurrences of in by .
- replace_at(, , ) = : This function returns a copy of , replacing the argument at by . must be a compound term.
C.2 Utilities on Strings and Lists
- chunks_of(, ) = : This function splits into chunks, each of which has length , and returns the list of the chunks. The last chunk may have fewer than elements if the length of is not a multiple of .
- drop(, ) = : This function returns the suffix of after the first elements are dropped, or [] if is greater than the length of .
- find(, , , ): This predicate searches for an occurrence of in , and binds to the starting index and to the ending index. On backtracking, this predicate searches for the next occurrence of .
- find_ignore_case(, , , ): This predicate is the same as find(, , , ), except that it is case insensitive.
- join() = : This function is the same as join(, " ").
- join(, ) = : This function concatenates the tokens into a string, adding , which is a string or an atom, between adjacent tokens.
- lstrip() = : This function is the same as lstrip(, " ' t' n' r").
- lstrip(, ) = : This function returns a copy of with leading elements in removed.
- power_set() = : This function returns the power set, i.e., a list of all subsets of a set represented as a list.
- rstrip() = : This function is the same as rstrip(, " ' t' n' r").
- rstrip(, ) = : This function returns a copy of with trailing elements in removed.
- split() = : This function uses whitespace characters as split characters. It is the same as split(, " ' t' n' r").
- split(, ) = : This function splits into a list of tokens, using characters in the string as split characters. Recall that a string is a list of characters. A token is a string, so the return value is a list of lists of characters.
- strip() = : This function is the same as strip(, " ' t' n' r").
- strip(, ) = : This function returns a copy of with leading and trailing elements in removed.
- take(, ) = : This function returns the prefix of of length , or itself if is greater than the length of .
C.3 Utilities on Matrices
An array matrix is a two-dimensional array. The first dimension gives the number of rows and the second dimension gives the number of columns. A list matrix represents a matrix as a list of lists.
- array_matrix_to_list() = : This function converts the array matrix to a flat list, row by row.
- array_matrix_to_list_matrix() = : This function converts the array matrix to a list matrix.
- columns() = : This function returns the columns of the matrix as a list.
- diagonal1() = : This function returns the primary diagonal of the matrix as a list.
- diagonal2() = : This function returns the secondary diagonal of the matrix as a list.
- list_matrix_to_array_matrix() = : This function converts the list matrix to an array matrix.
- matrix_multi(, ) = : This function returns the product of matrices and . Both and must be array matrices.
- rows() = : This function returns the rows of the matrix as a list.
- transpose() = : This function returns the transpose of the matrix . can be an array matrix or a list matrix. If is an array matrix, then the returned transpose is also an array. If is a list matrix, then the returned transpose is also a list.
C.4 Utilities on Lists and Sets
- nextto(, , ): This predicate is true if follows in . This predicate is nondeterministic.
- permutation(, ): This predicate generates a permutation of . This predicate is nondeterministic. On backtracking, it generates the next permutation.
- permutations() = : This function returns all the permutations of .