2

I need to put " " around a String in prolog. I get the input from another program and as it looks I can't escape the " in this program, so i have to add the " in prolog otherwise the prolog statement doesn't work.

Thanks for your help!

Wouter Beek
  • 3,307
  • 16
  • 29
Tim
  • 193
  • 4
  • 15
  • You can also consider using `atom_string/2`, e.g.: `?- atom_string(monkey, String).` results in `String = "monkey"`. – Wouter Beek Oct 22 '14 at 09:55
  • 1
    Thanks for your answer.I got the solution. I had the completly wrong approach. It was a change from SWI6 to SWI7. I installed SWI6.6 again and now it runs perfectly. – Tim Oct 22 '14 at 10:51
  • @Tim: There are some specific non ISO changes in SWI7 with respect to double quoted lists. – false Oct 22 '14 at 11:33
  • 2
    @false: exactly. I had than had this problem: **?- writeq('.'(a,[])). ERROR: Type error: `dict' expected, found `a'** Some information about the problem can be found her: http://www.complang.tuwien.ac.at/ulrich/iso-prolog/SWI7_and_ISO – Tim Oct 22 '14 at 11:48
  • 1
    Your question does not actually describe the problem. Some code would be highly useful. –  Oct 22 '14 at 13:07
  • 1
    @Tim I do not understand how switching to SWI6 has solved your problem. SWI6 does not support strings at all, so you would also not be able to put double quotes around them... – Wouter Beek Oct 22 '14 at 20:14
  • @WouterBeek: It all depends on what you mean by string. ISO supports the double quoted notation `"a"` ([see this](http://stackoverflow.com/a/8269897/772868)). And traditionally a list of character codes is a string. – false Jan 23 '15 at 21:27

1 Answers1

2

For a discussion of strings see here, they are SWI-Prolog specific but use the same escape rules as atoms. There are many ways to enter quotes into an atom in a Prolog text:

1) Doubling them. So for example 'can''t be' is an atom, with a single quote as the fourth character, and no other single quotes in it.

2) Escaping them, with the backslash. So for example 'can\'t be' is the same atom as 'can''t be'.

3) Character coding them, using octal code and a closing back slash. So for example 'can\47\t be' is the same atom as 'can''t be'.

4) Character coding them, using hex code and a closing back slash. So for example 'can\x27\t be' is the same atom as 'can''t be'.

The above possibilities are all defined in the ISO standard. A Prolog implementation might define further possibilities.

Bye

P.S.: Here is an example run in SWI-Prolog, using a different example character. In the first example query below, you don't need doubling, doubling can only be done for the surrounding quote.

The last example query below shows a SWI-Prolog specific syntax which is not found in the ISO standard, namely using a backslash u with a fixed width hex code:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.33)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam

?- X = 'she said "bye"'.
X = 'she said "bye"'.

?- X = 'she said \"bye\"'.
X = 'she said "bye"'.

?- X = 'she said \42\bye\42\'.
X = 'she said "bye"'.

?- X = 'she said \x22\bye\x22\'.
X = 'she said "bye"'.

?- X = 'she said \u0022bye\u0022'.
X = 'she said "bye"'.
Community
  • 1
  • 1
  • Which SWI-Prolog did you use above? And if 7.x, did you use the command line option "--traditional"? – repeat Aug 19 '15 at 16:24
  • Why do you ask? Is something wrong in my answer? The only non-ISO is the \uXXXX syntax. I don't know whether it existed already in SWI6 or is only present in SWI7. –  Aug 19 '15 at 16:27
  • I was just being curious! – repeat Aug 19 '15 at 16:28
  • I am not sure whether SWI-Prolog has additionally \UXXXXXXXX, oh hell it does: http://www.swi-prolog.org/pldoc/man?section=unicodesyntax . –  Aug 19 '15 at 16:33