Sometime term_string/2
confuses me, and it causes bugs into my codes. One of such confuses, I think, comes from that I believed that the length of “atom name” of the “atom” ‘A’ is 1. But, like an example below, the length of X returned by query term_string('A', X)
is 3 ! Maybe ‘A’ itself is not an atom, but merely a prolog term to indicate a unique atom which has name ‘A’ as a term. Of course, practically I am satisfied with nice property that
term_string(X, Y), term_string(Z, Y) => X == Z.
How are you free from possible confusions about term_string('A', X)
?
?- atom_length('A', X).
X = 1.
?- string_length('A', X).
X = 1.
?- term_string('A', X).
X = "'A'".
?- term_string('A', X), string_length(X, L).
X = "'A'",
L = 3.
?- term_string('a', X).
X = "a".
?- term_string(a, X).
X = "a".
?- term_string("abc", X), term_string(Y, X).
X = "\"abc\"",
Y = "abc".