问题描述
我是 Prolog 的新手,注意到 ' 和 " 给出不同的行为,但很好奇为什么.具体来说,在加载文件时,?- ['test1.pl'].
有效, 而 ?- ["test1.pl"].
没有.
单引号的项目总是原子.
双引号的含义取决于Prolog标志double_quotes
:
原子
—使用此值 a";= 一个代码>.如今,这很少使用.但是你会发现写有
["abc.pl"]
的 Prolog 书籍.
代码
—字符代码列表.这通常是默认值,但它会导致非常难以理解的答案,例如
更糟糕的是,如果您使用 ASCII 以外的字符:
?- 短语(("Я твой ",("слуга"|"работник"),"!"), Satz).萨茨 = [1071,32,1090,1074,1086,1081,32,1089,1083,1091,1075,1072,33];萨茨 = [1071,32,1090,1074,1086,1081,32,1088,1072,1073,1086,1090,1085,1080,1082,33].字符
— 单字符原子列表.查看此了解更多信息.
这个符号给出了更易读的答案,是 Scryer 和 Trealla 使用双引号表示法更紧凑地显示它们,用于打印任何单字符原子列表.对于 SICStus 和 SWI 这可以用下面的库来模拟.
?- use_module(library(double_quotes)).真的.?- 短语((Ja tvoi",(sluga"|rabotnik"),!"),Satz).Satz = Ja tvoi sluga!";Satz =Ja tvoi rabotnik!".?- 短语(("Я твой ",("слуга"|"работник"),"!"), Satz).Satz = "Я твой слуга!" ;Satz = "Я твой работник!".如果您在将 double_quotes.pl
作为库安装时遇到困难,只需将其放入其他 Prolog 文件的目录中并说:use_module(double_quotes).
I am new to Prolog and noticed that ' and " give different behavior, but am curious as to why. Specifically, when loading a file, ?- ['test1.pl'].
works, while ?- ["test1.pl"].
doesn't.
Single quoted items are always atoms.
The meaning of double quotes depends on the Prolog flag double_quotes
:
atom
—with this value "a" = a
. Nowadays, this is rarely used. But you will find Prolog books where ["abc.pl"]
is written.
codes
—a list of character codes. This is frequently the default, but it leads to very unreadable answers like
?- set_prolog_flag(double_quotes,codes). true. ?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz). Satz = [74,97,32,116,118,111,105,32,115,108,117,103,97,33] ; Satz = [74,97,32,116,118,111,105,32,114,97,98,111,116,110,105,107,33].
Even worse, if you use characters beyond ASCII:
?- phrase(("Я твой ",("слуга"|"работник"),"!"), Satz). Satz = [1071,32,1090,1074,1086,1081,32,1089,1083,1091,1075,1072,33] ; Satz = [1071,32,1090,1074,1086,1081,32,1088,1072,1073,1086,1090,1085,1080,1082,33].
chars
— a list of one-char atoms. See this for more about it.
?- set_prolog_flag(double_quotes,chars). true. ?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz). Satz = ['J',a,' ',t,v,o,i,' ',s,l,u,g,a,!] ; Satz = ['J',a,' ',t,v,o,i,' ',r,a,b,o,t,n,i,k,!]. ?- phrase(("Я твой ",("слуга"|"работник"),"!"), Satz). Satz = ['Я',' ',т,в,о,й,' ',с,л,у,г,а,!] ; Satz = ['Я',' ',т,в,о,й,' ',р,а,б,о,т,н,и,к,!].
This notation gives more readable answers and is the default in Scryer and Trealla which display them even more compactly with the double quote notation for printing any list of one-char atoms. For SICStus and SWI this can be emulated with the following library.
?- use_module(library(double_quotes)). true. ?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz). Satz = "Ja tvoi sluga!" ; Satz = "Ja tvoi rabotnik!". ?- phrase(("Я твой ",("слуга"|"работник"),"!"), Satz). Satz = "Я твой слуга!" ; Satz = "Я твой работник!".
If you have difficulties installing double_quotes.pl
as a library, simply put it into the directory of your other Prolog files and say: use_module(double_quotes).
这篇关于'和'有什么区别?在序言中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!