Atoms, Lists and Strings

This section includes predicates for identifying, comparing and manipulating atoms, lists and strings. The categories are:

  • Atom Processing
  • List Processing
  • String Processing
  • Atom Processing

    A number of predicates are provides for manipulating and converting atoms.

    atom_codes(Atom, CharList)

    atom_codes/2 is an alternative to the poorly "named" name/2 predicate. It converts between atoms and a list of character codes. If Atom is an atom then CharList is unified with the list of ASCII character codes for the name of Atom. If Atom is uninstantiated and CharList is a list of ASCII character codes, then instantiate Atom to the atom whose name is formed from CharList.

    atomlist_concat(AtomList, Atom)

    atomlist_concat/2 concatenates all of the atoms in AtomList to create a single atom, Atom.

    atom_concat(Atom1, Atom2, AtomVar3)

    atom_concat/3 concatenates atoms Atom1 and Atom2 and unifies that with variable AtomVar3. It can also generate all possible pairs of atoms, Atom1 and Atom2, from a given atom AtomVar3.

    atom_length(Atom, Length)

    atom_length/2 unifies the length of Atom with Length.

    atom_uplow(AtomUpper, AtomLower)

    atom_uplow/2 creates a new uppercase atom, AtomUpper, from a lowercase atom, AtomLower, and vice versa.

    current_atom(Atom)

    current_atom/1 succeeds by unifying Atom with an atom known to the Prolog system. On backtracking it unifies Atom with another atom and so on until all known atoms have been exhausted, in which case it fails.

    name(Atom, CharList)

    name/2 converts between atoms and a list of characters and is an alternative to atom_codes/2. If Atom is an atom then CharList is unified with the list of ASCII character codes for the name of Atom. If Atom is uninstantiated and CharList is a list of ASCII character codes, then instantiate Atom to the atom whose name is formed from CharList.

    sub_atom(Atom, Index, Length, SubAtom)

    sub_atom/4 extracts parts of atoms like sub_string/4 does for strings. The parameters are:

    Atom
    must be an atom
    Index
    the starting position, beginning with 1, of subatom
    Length
    the length of the subatom
    SubAtom
    the subatom

    In addition to the requirement that Atom be instantiated, either Index and Length must be instantiated, or SubAtom. In the first case the subatom is found, and in the second, the index andlength are found. Backtracking is fruitful in the second case if the subatom can be found more than once.

    If Index is instantiated and Length isn't, SubAtom becomes the rest of the Atom and Length is instantiated to its length.

    The instantiation requirements are somewhat more restrictive than the ISO standard which specifies that sub_atom can be used to generate all possible subatoms, with index and length, from a given atom. If you need/want this feature, let us know and we'll add it.

    Example:

    List Processing

    A list may have its elements sorted according to the standard ordering described above:

    compare_lists(List1, List2, DiffList)

    compare_lists compares two lists returning the items that appear in the first list, but not the second. List1 and List2 should be bound to lists. DiffList is unified with the elements in List1 that are not present in List2.

    is_member(Term, List)

    is_member is a restricted version of the classic member/2 predicate (in the LIST.PLM library) than can be used for fast testing if Term is a member of List. It uses a strong unify (==) for testing the element. The definition is equivalent to:

    Note, is_member cannot be backtracked into.

    remove_dups(List, NoDupsList)

    remove_dups removes duplicate entries from a list. List should be bound to a list. NoDupsList is unified with the list of distinct elements in List.

    sort(List, SortedList)

    sort sorts a list. List should be bound to a list. SortedList gets unified with the list whose elements are those of List arranged in ascending order with respect to @< above.

    String Processing

    A number of predicates are designed to work with strings.

    nonblank_string(String)

    nonblank_string/1 takes a String as its argument and tests to make sure it contains at least one non-whitespace character (ASCII > 32). It succeeds if the string is nonblank, and fails if it's blank.

    strcat(StringA, StringB, StringAB)

    strcat concatenates the first two strings to from the third. The first two arguments must be strings.

    stringlist_concat(StringList, String), stringlist_concat(StringList, Separator, String)

    stringlist_concat concatenates all of the strings or atoms in StringList to create the output String. For example:

    stringlist_concat/3 allows the use of a separator string that will appear between all of the elements in StringList

    string_atom(String, Atom)

    string_atom is used to convert between a string and an atom. If String is bound to a string, then Atom is unified with the corresponding atom. If Atom is bound to an atom, then String is unified with the string representation.

    string_icomp(String1, String2)

    string_icomp Performs a case insensitive compare of two strings.

    string_integer(String, Integer)

    string_integer converts back and forth between a string and its integer value (i.e., 33 and $33$).

    If String is bound to a string of ASCII digits, then Integer is bound to the corresponding value. If Integer is bound to a value, then String is bound to the ASCII digits representing that value. For example:

    string_length(String, Length)

    string_length requires that String be bound to a string. Length is unified with the length of the string. For example:

    string_list(String, List)

    string_list converts back and forth between a string and its representation as a list of ASCII characters (i.e., "foo" and $foo$).

    If String is bound to a string then string_list succeeds if it can unify List with the list of ASCII character codes of the characters in the string.

    If List is bound to a list of small integers (between 0 and 255) then this predicate succeeds if String can be unified with the string comprising the characters whose ASCII values are in List.

    For example:

    string_split(String, DelimitersS, List)

    string_split splits the String into a list of strings separated by the characters in the DelimiterS string. Unifies the result with List.

    Note, string_split differs from string_tokens in that it preserves whitespace, and does not return the delimiters as part of the list.

    string_term(String, Term)

    string_term converts back and forth between a string and a term. For example:

    string_termq(StringQ, Term)

    string_termq is exactly like string_term, except when creating a string from a term it quotes atoms that need it and puts $ $ around strings. This way the string can be converted to a term again. For example:

    string_tokens(String, TokenList), string_tokens(String, TokenList, DelimitersS)

    string_tokens takes a string and parses it into a list of tokens, where a token is a sequence of alphanumeric characters, or a punctuation mark. Whitespace between the tokens is removed. For example

    The three argument version allows you to specify the characters considered as punctuation marks. For example this query might be used to parse HTML strings:

    Note, string_tokens differs from string_split in that all delimiters are returned in the TokenList, and whitespace is not preserved.

    string_trim(String, TrimmedString)

    string_trim Trims the leading and trailing white space from String and unifies the resulting string with TrimmedString.

    sub_string(String, Index, Length, SubString)

    sub_string is used to locate or generate a substring of a given string. String must be bound to a string. There are two cases to consider:

    For example:

    Copyright ©1987-2000 Amzi! inc. All Rights Reserved.