From jan@swi.psy.uva.nl  Thu Jun 24 12:40:52 1999
Received: from gollem.swi.psy.uva.nl (jan@gollem.swi.psy.uva.nl [145.18.114.15])
	by swi.swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id MAA07252
	for <prolog@swi.psy.uva.nl>; Thu, 24 Jun 1999 12:40:52 +0200 (MET DST)
Received: from localhost (localhost [[UNIX: localhost]])
	by gollem.swi.psy.uva.nl (8.8.8/8.8.8) id MAA11228
	for prolog@swi.psy.uva.nl; Thu, 24 Jun 1999 12:42:25 +0200
From: Jan Wielemaker <jan@swi.psy.uva.nl>
To: prolog@swi.psy.uva.nl
Subject: Re: Confusing strings
Date: Thu, 24 Jun 1999 12:33:04 +0200
X-Mailer: KMail [version 0.7.9]
Content-Type: text/plain
References: <s772163c.077@sag.es>
MIME-Version: 1.0
Message-Id: <99062412422502.10642@gollem>
Content-Transfer-Encoding: 8bit

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

