current_prolog_flag double_quotes DCG(代码或字符)?
current_prolog_flag double_quotes DCG (codes or chars)?
在使用 SWI Prolog DCG 时,我注意到一些人注意到了
:- set_prolog_flag(double_quotes, codes).
而其他人注意到
:- set_prolog_flag(double_quotes, chars).
false, Markus,
DCG和短语一起使用有什么区别?
参考资料
The string type and its double quoted syntax
DCG
DCG Grammar rules
double_quotes
set_prolog_flag
正如名称 double_quotes
所暗示的那样,此标志会影响 双引号 (即:"
)的处理方式。
无论此类引号是否与 DCG 结合使用,都适用。但是,它在与 DCG 结合时特别有用,因为例如您可以使用:
?- phrase(nt(NT), "test").
从而自动将要解析的短语视为字符列表(在本例中:[t,e,s,t]
)。这非常适合交互式测试用例。
您链接到的 answer by false 解释得很好。另请注意答案中的以下引用:
This notation [using chars!] gives more readable answers. It can be even more compactly displayed since the double quote notation can be used for printing any list of one-char atoms.
显然 chars 比 codes
产生的答案更具可读性。例如:
?- Xs = "hello".
Xs = [h, e, l, l, o].
与 codes
:
?- Xs = "hello".
Xs = [104, 101, 108, 108, 111].
(咳咳.)
历史上,Han shot first 字符排在第一位!只是后来,这被更改为默认使用代码。在我看来,这是一个非常不幸的选择。 Haskell 等其他语言的工作方式与 Prolog 最初的工作方式相同:
Hugs> :t last
last :: [a] -> a
Hugs> :t "test"
"test" :: String
Hugs> last "test"
't'
在使用 SWI Prolog DCG 时,我注意到一些人注意到了
:- set_prolog_flag(double_quotes, codes).
而其他人注意到
:- set_prolog_flag(double_quotes, chars).
false, Markus,
DCG和短语一起使用有什么区别?
参考资料
The string type and its double quoted syntax
DCG
DCG Grammar rules
double_quotes
set_prolog_flag
正如名称 double_quotes
所暗示的那样,此标志会影响 双引号 (即:"
)的处理方式。
无论此类引号是否与 DCG 结合使用,都适用。但是,它在与 DCG 结合时特别有用,因为例如您可以使用:
?- phrase(nt(NT), "test").
从而自动将要解析的短语视为字符列表(在本例中:[t,e,s,t]
)。这非常适合交互式测试用例。
您链接到的 answer by false 解释得很好。另请注意答案中的以下引用:
This notation [using chars!] gives more readable answers. It can be even more compactly displayed since the double quote notation can be used for printing any list of one-char atoms.
显然 chars 比 codes
产生的答案更具可读性。例如:
?- Xs = "hello". Xs = [h, e, l, l, o].
与 codes
:
?- Xs = "hello". Xs = [104, 101, 108, 108, 111].
(咳咳.)
历史上,Han shot first 字符排在第一位!只是后来,这被更改为默认使用代码。在我看来,这是一个非常不幸的选择。 Haskell 等其他语言的工作方式与 Prolog 最初的工作方式相同:
Hugs> :t last last :: [a] -> a Hugs> :t "test" "test" :: String Hugs> last "test" 't'