list 在 prolog 脚本中定义列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10087265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:49:57  来源:igfitidea点击:

Defining lists in prolog scripts

listvariablesprolog

提问by woozle

I am new to prolog programming and have been told in a tutorial to define a list of structures (in a script) so that I can query it as a database. However I find it impossible to define this list as a variable in a script. When I define a list such as

我是 prolog 编程的新手,并且在教程中被告知要定义结构列表(在脚本中),以便我可以将其作为数据库进行查询。但是,我发现无法将此列表定义为脚本中的变量。当我定义一个列表时

X=[a,b,c].

I just receive an error saying

我刚刚收到一条错误消息

No permission to modify static_procedure `(=)/2'

Does prolog not support defining variables such as this? I am using SWI-Prolog under linux.

prolog 不支持定义这样的变量吗?我在 linux 下使用 SWI-Prolog。

回答by CapelliC

In Prolog we speak of logicalvariables, to mean identitybetween literals.

在 Prolog 中,我们谈到逻辑变量,意思是文字之间的同一性

That is, a program it's a set of rules that collectively state what's true about our literals, and that literals are uninterpreted. We write rules using variablesto describe relationsabout individuals, and while trying to prove if our query can become true, Prolog bindsvariables as rules dictate.

也就是说,程序是一组规则,共同说明我们的文字的真实性,并且文字是未解释的。我们使用变量来编写规则来描述关于个人的关系,并且在尝试证明我们的查询是否可以成为真时,Prolog按照规则的规定绑定变量。

A listit's just syntax sugar for a binary relation between a term (the head) and (note the recursion here) a list. Usually, when we speak of a database, we use facts (rules without a body, always true) that binds atomic literals.

一个列表它是一个项(之间的二元关系只是语法糖)和(这里要注意的递归)一个列表。通常,当我们谈到数据库时,我们使用绑定原子文字的事实(没有主体的规则,总是正确的)。

So that tutorial probably expresses the task in different words than you report, or it's somewhat misleading. You could anyway store lists in your database as such:

因此,该教程可能用与您报告的不同的词来表达任务,或者有点误导。您无论如何都可以将列表存储在您的数据库中:

mylist([a,b,c]).

and write your program like:

并编写您的程序,如:

myprog(X) :- mylist(L), member(X, L).

Then you can query your program like:

然后你可以像这样查询你的程序:

?- myprog(X).

and Prolog, trying to prove myprog/1, attempt to prove mylist/1 andmember/2... To prove mylist(L) the variableL get bound to [a,b,c].

和 Prolog,试图证明 myprog/1,试图证明 mylist/1member/2……为了证明 mylist(L),变量L 被绑定到 [a,b,c]。

HTH

HTH

回答by m09

When you write

当你写

X = [a, b, c].

It's read as

它被读作

=(X, [a, b, c]).

which is read as a definition of a fact concerning the =/2predicate. A fact where any free variable would be equal to [a, b, c]. That is, you redefine =/2. That's obviously not what you intend!

它被理解为关于=/2谓词的事实的定义。任何自由变量都等于 的事实[a, b, c]。也就是说,您重新定义=/2. 这显然不是你想要的!

You have to remember in Prolog that variables are scoped only locally, inside a predicate. What would work is:

您必须记住,在 Prolog 中,变量的作用域仅限于谓词内部的局部。什么会起作用:

main :-
    X = [a, b, c],
    % do stuff with X.

回答by CarlosGustavo

I use swipl under linux, to define a list in prolog.

我在linux下使用swipl,在prolog中定义一个列表。

mylist([element1,element2,elementn]).

Then you can query your program:

然后你可以查询你的程序:

?- mylist(A).

回答by Toni

If Y = [a,b,c], after the function makeList(Y,F) function call, F = [a,b,c]

如果 Y = [a,b,c],在函数 makeList(Y,F) 函数调用后,F = [a,b,c]

makeList(Y,F) :-
append(Y,[],X),
F = X.

e.g)

例如)

?- makeList([a,b,c],X).
X = [a,b,c].

回答by Thanos Tintinidis

no, you cannot do it like this. what are you basically writing is:

不,你不能这样做。你基本上写的是:

=(X,[a,b,x]).

and as the error says you cannot redefine =/2

正如错误所说,您无法重新定义 =/2

what you could do is:

你可以做的是:

x([a,b,c]).

and when you want to use X:

当你想使用 X 时:

...
x(X),
foo(X)
...