Prolog得到字符串的头和尾 [英] Prolog getting head and tail of string

查看:153
本文介绍了Prolog得到字符串的头和尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自己的大脑第一次围在Prolog上(SWI-Prolog),并且我为自己确定的基本知识而苦苦挣扎.我正在尝试使用诸如"pie"之类的字符串并打印出北约的军事拼写,使其看起来像这样:

I'm trying to wrap my brain around Prolog for the first time (SWI-Prolog) and I'm struggling with what I'm sure are the basics. I'm trying to take a string such as "pie" and print out the military NATO spelling of it to look something like this:

spellWord("Pie").
Papa
India
Echo

当前,我只是在尝试验证是否正确使用了[H | T]语法和Write函数.我的功能是:

Currently I'm just trying to verify that I'm using the [H|T] syntax and Write function correctly. My function is:

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

writeChar(String) :- H == "P", print4("Papa").

拨打spellWord("Pie")时.当前仅返回false.

When making a call to spellWord("Pie"). this currently just returns false.

推荐答案

SWI-Prolog对您可能称为字符串"的表示形式有所不同.

SWI-Prolog has several different representation of what you might call "strings".

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

您应该阅读该文档,但是到目前为止,您至少有两种选择.

You should read the documentation, but for now, you have at least two choices.

选择1 :使用标记来创建双引号的字符串代码列表

Choice 1: Use a flag to make double-quoted strings code lists

$ 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].

此时,您的方法应该可以使用了,因为您现在有了列表.

At this point, your approach should work, as you now have a list.

选择2 :使用带有反引号的新代码列表语法

Choice 2: Use the new code list syntax with back-ticks

?- X = `abc`.
X = [97, 98, 99].


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


And, of course, there are predicates that convert between atoms, code lists, char lists, and strings. So, to make a list of chars (one-character atoms), you have:

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

对于谓词定义,请考虑在头部使用统一.另外,请勿将副作用(打印)与谓词混合使用.让顶层(Prolog解释器)为您完成打印.

As for your predicate definition, consider using unification in the head. Also, don't mix side effects (printing) with what the predicate does. Let the top level (the Prolog interpreter) do the printing for you.

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).

并以此:

?- word_nato(`Pie`, Nato).
Nato = ['Papa', 'India', 'Echo'].

我使用字符(一个字母的原子)代替了字符代码,因为它们更容易编写.

I used chars (one-letter atoms) instead of character codes because those are easier to write.

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

And finally, you can use the following flag, and set_prolog_flag/2 at run time to change how Prolog treats a string enclosed in double quotes.

例如:

$ 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.

这篇关于Prolog得到字符串的头和尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆