用于完成字符串的 Prolog 通配符

编程入门 行业动态 更新时间:2024-10-26 01:17:20
本文介绍了用于完成字符串的 Prolog 通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前遇到了一个序言问题.

I am currently stuck on a prolog problem.

到目前为止我有:

film(Title) :- movie(Title,_,_). (其中 'movie(T,_,_,)' 是对我的数据库)

film(Title) :- movie(Title,_,_). (Where 'movie(T,_,_,)' is a reference to my database)

namesearch(Title, Firstword) :- film(Title), contains_term(Firstword, Title).

很难解释我需要什么帮助,但基本上是否有一个通配符可以用来搜索以特定单词开头的所有电影,例如,如果我要搜索所有以单词开头的电影"的".

It is hard to explain what I need help on, but basically is there a wildcard I can use to search for all films starting with a specific word, for example, if I were to search for all films beginning with the word "The".

是否有通配符允许我输入:namesearch(X,'The*')?

Is there a wildcard which would allow me to input as such: namesearch(X,'The*') ?

我试过像这样使用星号,但它不起作用,

I have tried using the asterisk like this and it does not work,

感谢您的帮助

推荐答案

这完全取决于标题的表示方式.

It all depends how the title is represented.

如果表示为原子,则需要sub_atom(Atom, Before, Length, After, Sub_atom)

If it is represented as an atom, you need sub_atom(Atom, Before, Length, After, Sub_atom)

?- Title = 'The Third Man', sub_atom(Title, 0, _, _, 'The'). Title = 'The Third Man'.

代码列表

如果它是一个代码列表,在爱丁堡传统的 Prologs 中称为字符串,您可以使用 append/3 对其进行硬编码",或者您可以使用 Definite Clause Grammars 表示一般模式.

List of codes

If it is a list of codes which is called a string in Prologs in Edinburgh tradition, you can either "hard code" it with append/3 or you might use Definite Clause Grammars for general patterns.

?- set_prolog_flag(double_quotes,codes). true. ?- append("The",_, Pattern), Title = "The Third Man", Pattern = Title. Pattern = Title, Title = [84, 104, 101, 32, 84, 104, 105, 114, 100|...]. ?- Title = "The Third Man", phrase(("The",...), Title). Title = [84, 104, 101, 32, 84, 104, 105, 114, 100|...] ; false.

注意84是T等的字符代码

Note that 84 is the character code of T etc.

phrase/2 是语法的入口".有关更多信息,请参阅 dcg.上面使用了以下定义:

phrase/2 is "the entry" to grammars. See dcg for more. Above used the following definition:

... --> [] | [_], ... .

字符列表

类似于代码列表,字符列表提供了一种更具可读性的表示,它仍然具有与列表谓词和定语从句语法兼容的优点:

List of characters

Similar to list of codes, list of characters provide a more readable representation that has still the advantages of being compatible with list predicates and Definite Clause Grammars:

?- set_prolog_flag(double_quotes,chars). true. ?- append("The",_, Pattern), Title = "The Third Man", Pattern = Title. Pattern = Title, Title = ['T', h, e, ' ', 'T', h, i, r, d|...]. ?- Title = "The Third Man", phrase(("The",...), Title). Title = ['T', h, e, ' ', 'T', h, i, r, d|...] ; false.

另见这个答案.

更多推荐

用于完成字符串的 Prolog 通配符

本文发布于:2023-10-24 14:56:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   字符串   Prolog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!