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
  • Character List Processing
  • Atom Processing

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

    atom_codes(Atom, CharList)

    atom_codes/2 converts between atoms and a list of character codes. If Atom is an atom then CharList is unified with the list of character codes for the name of Atom. If Atom is uninstantiated and CharList is a list of 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 the list 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.


    is_atom/1 succeeds if X is an atom.

    name(Atom, CharList)

    name/2 is the same as atom_codes/2. It is preserved because it is used in many old Prolog programs.

    sub_atom(Atom, Index, Length, SubAtom)

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

    must be an atom
    the starting position, beginning with 1, of subatom
    the length of the 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.


    List Processing

    Some list utility predicates are basic built-in predicates. There are many more that are part of the list library. These are the built-in ones.

    is_member(Term, List)

    is_member/2 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. It cannot be used to backtrack through the various members of a list. The definition is equivalent to:

    sort(List, SortedList)

    sort/2 sorts a list. List should be bound to a list. SortedList gets unified with the list whose elements are those of List arranged according to standard order.

    keysort(KeyedList, KeyedSortedList)

    keysort/2 sorts a list of keyed elements of the form Key-Item. KeyedSortedList should be bound to a list. KeyedSortedList gets unified with the list whose elements are those of KeyedList sorted by key in standard order.

    String Processing

    A number of predicates are designed to work with strings.


    is_string/1 succeeds if X is a string.


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

    strcat(StringA, StringB, StringAB)

    strcat/3 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/2 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/2 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. The more general string_term/2 can be used as well.

    string_float(String, Float)

    string_float/2 is used to convert between a string and a floating point number.

    string_icomp(String1, String2)

    string_icomp/2 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`). The more general string_term/2 can be used as well.

    If String is bound to a string of 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_number(String, Number)

    string_number converts back and forth between a string and its numeric value.

    string_length(String, Length)

    string_length/2 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/2 converts back and forth between a string and its representation as a list of character codes(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 character codes of the characters in the string.

    If List is bound to a list of character codes then this predicate succeeds if String can be unified with the string comprising the characters whose codes are in List.

    For example:

    string_list/2 It is especially useful for applications that do character-by-character parsing on input/output strings. DCG applications often use string_list/2. The following example uses some list library predicates to add a file extension to a string file name if it doesn't have one.

    Trying it:

    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/3 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/2 converts back and forth between a string and a term. For example:

    string_term/2 uses the normal Prolog I/O reader to read/write the term to/from the string. You can use catch/3 to catch bad syntax in the string as the following example shows:

    Trying it:

    string_termq(StringQ, Term)

    string_termq/2 is exactly like string_term/2, except when creating a string from a term it adds syntax like quotes as necessary to ensure that the string can be converted back to the same term. For example, an atom with a leading uppercase letter needs to be quoted so it isn't confused as a variable. For example:

    is_string_term(String, Term)

    is_string_termq/2 is exactly like string_term/2, except instead of throwing an error when an input string has a syntax error, it simply fails.

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

    string_tokens/2,3 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 with one exception, an ending period. An period embedded in text is simply a period, but one followed by white space is period space ( '. ' ). 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/2,3 differs from string_split/3 in that all delimiters are returned in the TokenList, and whitespace is not preserved.

    string_trim(String, TrimmedString)

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

    sub_string(String, Index, Length, SubString)

    sub_string/4 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:

    tilt_slashes(InAS, OutAS)

    tilt_slashes/2 converts between slashes of either tilt in the string In, to the correct path separator slashes for the platform, which is / on Unix and \ on Windows. Note that this is not usually necessary because the file open predicates accept either tilt on slashes.

    For example, this was run on a Windows version of Amzi!:

    Character List Processing

    A number of predicates are designed to work with character lists.

    atom_chars(Number, CharList)

    atom_chars/2 converts back and forth between an atom and a list of characters.


    char/1 succeeds if X is a character.

    char_code(Atom, CharList)

    char/1 converts back and forth between an atom and a list of characters.

    number_chars(Number, CharList)

    number_chars/2 converts back and forth between a number and a list of characters.

    Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.