Re: Difficulties with \

Pierpaolo Bernardi <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <CANY8u7EyePYdpmaOXKHPgOjm5_nScYcYsznx5V+=e3vjcdrCYQ@mail.gmail.com>
On Sat, Oct 26, 2013 at 12:14 AM, joel <[email protected]> wrote:
> Hello all
>
> How can I create atom \sin ?

If you want the atom whose name is composed by the four characters \,
s, i, n, then the literal syntax is '\\sin'

> I get for example
>
> ?- writef('%n%w', [92,sin]).
> \sin
> true.

You just wrote the characters.

> ?- with_output_to(atom(A), writef('%n%w', [92,sin])).
> A = '\\sin'.

that's the atom you want. You can check with writeln:

16 ?- writeln('\\sin').
\sin
true.

or with atom_chars:

18 ?- atom_chars('\\sin',C).
C = [\, s, i, n].

> Idem :
>
> ?- A= \sin.
> A = \sin.

This is not an atom:

11 ?- atom(\sin).
false.
12 ?- write_canonical(\sin).
\(sin)
true.

> ?- A= \sin, atomic_list_concat([A, '(', 8, ')'], L).
> ERROR: atomic_list_concat/2: Type error: `text' expected, found `\sin'

right. since A is not an atom.

> ?- A= '\sin', atomic_list_concat([A, '(', 8, ')'], L).
> A = ' in',
> L = ' in(8)'.

now A is an atom, but \s in a quoted atom name is an escape sequence
for the space character.

> ?- A= '\\sin', atomic_list_concat([A, '(', 8, ')'], L).
> A = '\\sin',
> L = '\\sin(8)'.
>
> Is it a bug ?

No. It's all working fine.  Read section '2.15.1.2 Character Escape
Syntax' in the manual for the full details.

P.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.