4.8.4 Converting between Constants and Text

Three predicates convert between constants and lists of character codes: atom_codes/2, number_codes/2, and name/2. Two predicates convert between constants and lists of character atoms: atom_chars/2, number_chars/2.

atom_codes(Atom, Codes) is a relation between an atom Atom and a list Codes consisting of the character codes comprising the printed representation of Atom. Initially, either Atom must be instantiated to an atom, or Codes must be instantiated to a proper code-list.

number_codes(Number, Codes) is a relation between a number Number and a list Codes consisting of the character codes comprising the printed representation of Number. Initially, either Number must be instantiated to a number, or Codes must be instantiated to a proper code-list.

Similarly, atom_chars(Atom, Chars) and number_chars(Atom, Chars) are relations between a constant and a list consisting of the character atoms comprising the printed representation of the constant.

name/2 converts between a constant and a code-list. Given a code-list, name/2 will convert it to a number if it can, otherwise to an atom. This means that there are atoms that can be constructed by atom_codes/2 but not by name/2. name/2 is retained for backwards compatibility with other Prologs. New programs should use atom_codes/2 or number_codes/2 as appropriate.

char_code/2 converts between a character atom and a character code.


Send feedback on this subject.