Prolog得到字符串的头尾

don*_*age 7 string list prolog dcg

我正试图将我的大脑第一次包裹在Prolog(SWI-Prolog),我正在努力克服我确定的基础知识.我正试图拿一个像"馅饼"这样的字符串并打印出军事北约拼写它看起来像这样:

spellWord("Pie").
Papa
India
Echo
Run Code Online (Sandbox Code Playgroud)

目前我只是想验证我正在使用[H | T]语法和Write函数.我的功能是:

spellWord(String) :- String = [H|T], writeChar(H), spellWord(T).

writeChar(String) :- H == "P", print4("Papa").
Run Code Online (Sandbox Code Playgroud)

打电话时spellWord("Pie").这当前只返回false.

fal*_*lse 7

无论您使用的是Prolog系统,除非您必须维护现有代码,否则请坚持使用set_prolog_flag(double_quotes, chars).这适用于许多系统, 如B,GNU,IF,IV,Minerva,SICStus,SWI,YAP.所以这是一个安全的赌注.@Boris提到的其他选项很难调试.一个甚至仅针对SWI.

?- set_prolog_flag(double_quotes, chars).
true.

?- L = "abc".
L = [a, b, c].
Run Code Online (Sandbox Code Playgroud)

library(double_quotes) 这些字符串可以更紧凑地打印.

在SWI中,您可以做的最好的事情是.swiplrc:

:- set_prolog_flag(back_quotes, string).
:- set_prolog_flag(double_quotes, chars).
:- use_module(library(double_quotes)).
Run Code Online (Sandbox Code Playgroud)

对于您的具体示例,最好立即避免产生副作用.而是考虑定义单词和拼写之间的关系:

word_spelling(Ws, Ys) :-
   phrase(natospelling(Ws), Ys).

natospelling([]).
natospelling([C|Cs]) -->
   {char_lower(C, L)},
   nato(L),
   "\n",
   natospelling(Cs).

nato(p) --> "Papa".
nato(i) --> "India".
nato(e) --> "Echo".

char_lower(C, L) :-
   char_type(L, to_lower(C)).

?- word_spelling("Pie",Xs).
Xs = "Papa\nIndia\nEcho\n".

?- word_spelling("Pie",Xs), format("~s",[Xs]).
Papa
India
Echo
Xs = "Papa\nIndia\nEcho\n".
Run Code Online (Sandbox Code Playgroud)

这是你原来的定义.然而,大部分时间都坚持使用它的纯粹核心.

spellWord(Ws) :-
   word_spelling(Ws, Xs),
   format("~s", [Xs]).
Run Code Online (Sandbox Code Playgroud)

另请注意,SWI的内置功能library(pio)仅适用于代码,并且会打开不必要的选择点.相反,请使用此替代品 ,该替代品适用于charscodes取决于Prolog标志.

历史上,字符首先表示为长度为1的原子.也就是说,1972年在Prolog 0中.然而,在那里,字符串以左关联方式表示,这有利于后缀匹配.

plur(nil-c-i-e-l, nil-c-i-e-u-x).
Run Code Online (Sandbox Code Playgroud)

从1973年的Prolog I开始,双引号意味着像今天一样的字符列表.

1977年,DECsystem 10 Prolog将双引号的含义更改为字符代码列表,并使用代码代替字符.这使得一些I/O操作更有效率,但是调试这样的程序要困难得多[76,105,107,101,32,116,104,105,115] - 你能读懂它吗?

ISO Prolog支持两者.有一个标志double_quotes指示如何解释双引号.此外,两个字符相关的内置函数都存在:

char_code/2

atom_chars/2, number_chars/2, get_char/1/2, peek_char/1/2, put_char/1/2

atom_codes/2, number_codes/2, get_code/1/2, peek_code/1/2, put_code/1/2
Run Code Online (Sandbox Code Playgroud)


小智 6

SWI-Prolog有几种不同的表示形式,你可以称之为"字符串".

  • 字符代码列表(Unicode);
  • 字符列表(单字母原子);
  • 字符串,它们是"原子"对象,只能使用字符串的内置谓词进行操作;
  • 最后,当然,原子.

您应该阅读文档,但是现在,您至少有两个选择.

选择1:使用标志来制作双引号字符串代码列表

$ swipl --traditional
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.19-57-g9d8aa27)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- X = "abc".
X = [97, 98, 99].
Run Code Online (Sandbox Code Playgroud)

此时,您的方法应该有效,因为您现在有一个列表.

选择2:使用带有反向标记的新代码列表语法

?- X = `abc`.
X = [97, 98, 99].
Run Code Online (Sandbox Code Playgroud)

当然,有些谓词可以在原子,代码列表,字符列表和字符串之间进行转换.因此,要制作一个字符列表(一个字符的原子),你有:

  • atom_chars/2
  • char_code/2
  • string_chars/2

至于你的谓词定义,考虑在头部使用统一.另外,不要将副作用(打印)与谓词的作用混合在一起.让顶级(Prolog解释器)为您进行打印.

nato(p, 'Papa').
nato(i, 'India').
nato(e, 'Echo').
% and so on

word_nato([], []).
word_nato([C|Cs], [N|Ns]) :-
    char_code(Char, C),
    char_type(U, to_lower(Char)),
    nato(U, N),
    word_nato(Cs, Ns).
Run Code Online (Sandbox Code Playgroud)

有了这个:

?- word_nato(`Pie`, Nato).
Nato = ['Papa', 'India', 'Echo'].
Run Code Online (Sandbox Code Playgroud)

我使用字符(单字母原子)而不是字符代码,因为它们更容易编写.


最后,您可以使用以下标志,并set_prolog_flag/2在运行时更改Prolog如何处理用双引号括起来的字符串.

例如:

$ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.19-40-g2bcbced)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- current_prolog_flag(double_quotes, DQs).
DQs = string.

?- string("foo").
true.

?- set_prolog_flag(double_quotes, codes).
true.

?- X = "foo".
X = [102, 111, 111].

?- set_prolog_flag(double_quotes, chars).
true.

?- X = "foo".
X = [f, o, o].

?- set_prolog_flag(double_quotes, atom).
true.

?- X = "foo".
X = foo.
Run Code Online (Sandbox Code Playgroud)