next up previous
Next: Operators Up: Built-In Predicates Previous: Analysing and Constructing

Representing Text in Strings

  SWI-Prolog supports the data type string. Strings are a time and space efficient mechanism to handle text in Prolog. Atoms are under some circumstances not suitable because garbage collection on them is next to impossible (Although it is possible: BIM_prolog does it). Representing text as a list of ASCII values is, from the logical point of view, the cleanest solution. It however has two drawbacks: 1) they cannot be distinguished from a list of (small) integers; and 2) they consume (in SWI-Prolog) 12 bytes for each character stored.

Within strings each character only requires 1 byte storage. Strings live on the global stack and their storage is thus reclaimed on backtracking. Garbage collection can easily deal with strings.

The ISO standard proposes "..." is transformed into a string object by read/1 and derivates. This poses problems as in the old convention "..." is transformed into a list of ascii characters. For this reason the style check option ` string' is available (see style_check/2).

The set of predicates associated with strings is incomplete and tentative. Names and definitions might change in the future to confirm to the emerging standard.

string_to_atom( ?String, ?Atom)

  Logical conversion between a string and an atom. At least one of the two arguments must be instantiated. Atom can also be an integer or floating point number.
string_to_list( ?String, ?List)

  Logical conversion between a string and a list of ASCII characters. At least one of the two arguments must be instantiated.
string_length( +String, -Length)

  Unify Length with the number of characters in String. This predicate is functonally equivalent to atom_length/2 and also accepts atoms, integers and floats as its first argument.
substring( +String, +Start, +Length, -Sub)

  Create a substring of String that starts at character Start (1 base) and has Length characters. Unify this substring with Sub. gif



next up previous
Next: Operators Up: Built-In Predicates Previous: Analysing and Constructing



Passani Luca
Tue Nov 14 08:58:33 MET 1995