[ Term Manipulation | Reference Manual | Alphabetic Index ]

term_string(?Term, ?String)

Conversion between a Prolog term and a string.
Term
Prolog term.
String
String or a variable.

Description

If String is instantiated, its contents are parsed, and if the whole string can be parsed as one Prolog term it is unified with Term. If String is not instantiated, Term is written into a string (using writeq/2) and String is bound to it.

To customize the way the term is converted into a string, e.g. to include attributed variable print handlers, use term_string/3 with appropriate Options, or use sprintf/3.

Modes and Determinism

Modules

This predicate is sensitive to its module context (tool predicate, see @/2).

Exceptions

(5) type error
String is instantiated, but not to a string.
(7) string contains unexpected characters
String cannot be converted to a Prolog term.

Examples

Success:
      term_string(T, "look").      (gives T=look).
      term_string(T, "26.0").      (gives T=26.0).
      term_string(T, "f(1,2).").   (gives T=f(1,2)).
      term_string(T, "f(1,2)").    (gives T=f(1,2)).
      term_string(f(1,2),L).       (gives L="f(1, 2)").
      term_string(f(1,2),"f(1, 2)").
      term_string(atom,S).         (gives S="atom").
      term_string(.(a,.(1,[])),S). (gives S="[a, 1]").
      term_string(2.60,"2.6").
      term_string(2.6,"2.60").
      term_string(T,S).            (gives T=_g94; S="_g94").

Fail: term_string(2.6,"2.5").

Error:
      term_string(T,atom).              (Error 5).
      [eclipse]: term_string(T,"F(1,2)").  % String not a string
      F(1,2)                               % of a prolog term
       ^ (here?)
      syntax error: unexpected token
      string contains unexpected characters in term_string(T, "F(1,2)")

See Also

term_string / 3, number_string / 2, read / 2, writeq / 2, sprintf / 3