swipldcgtut
swipldcgtut copied to clipboard
strings in queries in examples
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=~w~n', [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.