我自己学习Prolog。我有一个看起来像这样的文本文件:
table("0","Nicosia 2013 Vulkà Bianco (Etna)","Italy","White Blend","14","Kerin O’Keefe","87").
table("1","Quinta dos Avidagos 2011 Avidagos Red (Douro)","Portugal","Portuguese Red","15","Roger Voss","87").
table("2","Rainstorm 2013 Pinot Gris (Willamette Valley)","US","Pinot Gris","14","Paul Gregutt","87").
table("3","St. Julian 2013 Reserve Late Harvest Riesling (Lake Michigan Shore)","US","Riesling","13","Alexander Peartree","87").
我有400行,我想在序言中声明这些行。我尝试了几个选项,而这段代码是最后一个:
load_file_data(File) :-
open(File, read, Stream),
repeat,
read(Stream, Term),
( Term = end_of_file
-> true
; assert(Term),
fail
),
close(Stream).
此版本和其他一些版本断言,但不是我期望的那样。这就是断言的样子:
table([48], [78, 105, 99, 111, 115, 105, 97, 32, 50, 48, 49, 51, 32, 86, 117, 108, 107, 195, 402, 194, 160, 32, 66, 105, 97, 110, 99, 111, 32, 32, 40, 69, 116, 110, 97, 41], [73, 116, 97, 108, 121], [87, 104, 105, 116, 101, 32, 66, 108, 101, 110, 100], [49, 52], [75, 101, 114, 105, 110, 32, 79, 195, 162, 226, 8218, 172, 226, 8222, 162, 75, 101, 101, 102, 101], [56, 55]).
table([49], [81, 117, 105, 110, 116, 97, 32, 100, 111, 115, 32, 65, 118, 105, 100, 97, 103, 111, 115, 32, 50, 48, 49, 49, 32, 65, 118, 105, 100, 97, 103, 111, 115, 32, 82, 101, 100, 32, 40, 68, 111, 117, 114, 111, 41], [80, 111, 114, 116, 117, 103, 97, 108], [80, 111, 114, 116, 117, 103, 117, 101, 115, 101, 32, 82, 101, 100], [49, 53], [82, 111, 103, 101, 114, 32, 86, 111, 115, 115], [56, 55]).
table([50], [82, 97, 105, 110, 115, 116, 111, 114, 109, 32, 50, 48, 49, 51, 32, 80, 105, 110, 111, 116, 32, 71, 114, 105, 115, 32, 40, 87, 105, 108, 108, 97, 109, 101, 116, 116, 101, 32, 86, 97, 108, 108, 101, 121, 41], [85, 83], [80, 105, 110, 111, 116, 32, 71, 114, 105, 115], [49, 52], [80, 97, 117, 108, 32, 71, 114, 101, 103, 117, 116, 116], [56, 55]).
这样的结果返回true:
?- table("1","Quinta dos Avidagos 2011 Avidagos Red(Douro)","Portugal","Portuguese Red","15","Roger Voss","87").
true.
当我尝试获取字段时,结果如下:
table("364",X,"US","Pinot Noir","48","Paul Gregutt","95").
X = [87, 105, 110, 100, 101, 114, 108, 101, 97|...] ;
false.
答案 0 :(得分:1)
我解决了这个问题。实际上,这是一个非常简单的问题。
从版本7开始,字符串对象的语法为text 在双引号之间,例如“ hello”。
如版本7之前的文档所述,SWI Prolog不接受双引号引起来的字符串。由于种种原因,我最近切换到了6.6.5版。所以我只是解决了这个问题,我使用单引号而不是双引号,并将字符串中的所有单引号更改为`。
答案 1 :(得分:1)
Prolog中双引号文字的含义取决于标准的double_quotes
标志。该标准指定了三个可能的值:codes
(字符代码列表),atom
和chars
(单字符原子列表)。某些Prolog系统,例如SWI-Prolog实现其他值,例如string
。您可以通过调用以下命令来检查标志的默认值:
?- current_prolog_flag(double_quotes, Value).
您可以使用set_prolog_flag/2
谓词来设置默认标志值。您还可以在包含双引号的子句之前使用set_prolog_flag/2
伪指令为Prolog文件的内容设置标志值。例如:
:- set_prolog_flag(double_quotes, atom).
最便于携带的标志值为codes
,它对应于双引号文本的传统解释。