Subject: Re: Confusing strings

From: Jan Wielemaker <[email-masked]>
Date: Thu, 24 Jun 1999 12:33:04 +0200

On Thu, 24 Jun 1999, Sergio Arias Sanchez wrote:

>  I've been using strings in my programs and I found some difficulties using the 
>related predicates.
>  First it seems that is not the same a STRING "hello" than the LIST "hello". Both 
>look the same, but...
>
>  ?- string("hello")
>      No

Normally, SWI-Prolog reads "bla" as a list of ascii characters.  If
style_check(+string) is active, it is read as a string object.  This
isn't really encouraged.

>  ?- string_to_list(Str,"hello"), string(Str).
>      Str = "hello"
>      Yes
>
>So far, so good... But:
>
>  ?- string_to_atom("hello", A).
>      A = hello
>      Yes

Actually all XYX_to_atom/2 work the same if you instantiate the
XYZ, they just check the argument type, convert it to characters
and creates (or lookup) an atom for it.  Only working the other
way around, these predicates make an object of the requested
type:

?- string_to_atom(45, X)

X = '45'.

> So now "hello" is not a list but a string !!?
>
>  It would be ok if we could treat strings as either lists of ascii values or as types, 
>but following the same criteria.

Strings have been in the ISO proposal someday, but have gone since
long.  As SWI-Prolog lacks atom-garbage collection, strings are a
suitable mechanism to represent dynamically created character
strings that have a short life-span.  They are also suitable for
dealing with arbitrary binary data created from and handled from
foreign code whose life-span should be controlled from Prolog
(for example representing arbitrary-precision arithmetic).

Otherwise, if you want to reason about the characters, use lists
and if atom garbage isn't a real problem, use atoms.

	Regards --- Jan