Prolog获取字符串的头部和尾部 [英] Prolog getting head and tail of string

查看:10
本文介绍了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

至于您的谓词定义,请考虑在头部使用统一.此外,不要将副作用(打印)与谓词的作用混为一谈.让顶层(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天全站免登陆