Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strings in queries in examples #9

Open
Anniepoo opened this issue Oct 3, 2016 · 0 comments
Open

strings in queries in examples #9

Anniepoo opened this issue Oct 3, 2016 · 0 comments

Comments

@Anniepoo
Copy link
Owner

Anniepoo commented Oct 3, 2016

Dear Anne,

While refreshing my knowledge of DCGs, I stumbled on the double quoted strings,
which - at least now and on a mac - (Multi-threaded, 64 bits, Version 7.2.3)
are not by default a list of character codes. Probably this complicated chapter
is relevant here: http://www.swi-prolog.org/pldoc/man?section=strings

In http://www.pathwayslms.com/swipltuts/dcg/#anch1 you have this DCG
(but also check the cliche example)

fizz_buzz(Msg) --> anything, fizz(Msg), anything, buzz, anything.
anything --> [].
anything --> [_], anything.
fizz(Msg) -->
"fizz",
{
format('At fizz we have Msg=wn', [Msg])
}.
buzz -->
"buzz".

Now trying this:

Str = "fizzbuzz”, phrase(fizz_buzz(patat),Str).

results in:

ERROR: Type error: list' expected, found"fizzbuzz"' (a string)

This though, works:

?- phrase(fizz_buzz(patat),X).
At fizz we have Msg=patat
X = [102, 105, 122, 122, 98, 117, 122, 122] ;

AND also:

phrase(fizz_buzz(patat),X).[102, 105, 122, 122, 98, 117, 122, 122]).

The first one only works after
?- set_prolog_flag(double_quotes, codes).
true.
As we see here:
?- phrase(fizz_buzz(patat),"fizzbuzz").
At fizz we have Msg=patat
true

Now may we conclude that inside DCG’s SWI Prolog always interprets
double quoted strings as lists of character codes, but in a QUERY you’d
better set your double_quotes flag properly! An example of which
here would certainly save time in searching the dense manual BTW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant