简体   繁体   English

声明一个变量

[英]Declare a variable

I didn't touch Prolog since high-school, and even though I've tried to find the info, it didn't help. 从高中开始我就没有接触过Prolog,即使我尝试查找信息,也无济于事。 Below is the example that has to illustrate my problem: 下面是必须说明我的问题的示例:

%% everybody():- [dana, cody, bess, abby].
%% Everybody = [dana, cody, bess, abby].

likes(dana, cody).
hates(bess, dana).
hates(cody, abby).

hates(X, Y):- \+ likes(X, Y).

likes_somebody(_, []):- fail.
likes_somebody(X, [girl | others]):-
    likes(X, girl) ; likes_somebody(X, others).

likes_everybody(_, []):- true.
likes_everybody(X, [girl | others]):-
    likes(X, girl) , likes_everybody(X, others).

maplist(likes_somebody, [dana, cody, bess, abby], [dana, cody, bess, abby]).

How do I declare everybody to just be the list of girls? 我如何宣布everybody只是女孩名单? The commented lines are those which I've tried, but I've got bizarre error messages back. 带注释的行是我尝试过的行,但是我收到了奇怪的错误消息。

This is the tutorial I followed more or less so far. 到目前为止,这是我或多或少遵循的教程 I'm using GProlog, if it makes any difference. 我正在使用GProlog,如果有什么不同的话。 Sorry for such a basic question. 很抱歉有这样一个基本问题。 GProlog's manual doesn't deal with language syntax, but I've certainly looked there. GProlog的手册不涉及语言语法,但是我确实看过那里。 As an aside, I would be grateful for information on where to look for language documentation (as opposed to implementation documentation). 顺便说一句,对于在哪里可以找到语言文档(而不是实现文档)的信息,我将不胜感激。

Every variable in Prolog must begin with an uppercase letter. Prolog中的每个变量都必须以大写字母开头。 So for starters, you want Everybody , not everybody . 因此,对于初学者来说,您想要Everybody ,而不是everybody

Second problem, variables in Prolog are not assignables . 第二个问题,Prolog中的变量不是可分配的 So probably what you want to do is make a fact and use that instead: 因此,您可能想做的是建立一个事实,并使用该事实:

everybody([dana, cody, bess, abby]).

Your bottom line of code is actually a fact definition and will attempt to overwrite maplist/3 . 您的代码底线实际上是事实定义,并且将尝试覆盖maplist/3 What you probably want to do is put everything above that line into a file (say, called likes.pl ) and then consult it ( [likes]. ). 您可能想要做的是将该行上方的所有内容放入文件(例如,称为likes.pl ),然后进行查询( [likes]. )。 Then you can run a query like this: 然后,您可以运行如下查询:

?- everybody(Everybody), maplist(likes_somebody, Everybody, Everybody).

This won't work, because likes_somebody/2 processes a list in the second argument. 这将不起作用,因为likes_somebody/2处理第二个参数中的列表。 The predicate you have for likes_somebody/2 could be written: 您可以为likes_somebody/2编写谓词:

likes_somebody(_, []).

but still won't mean much. 但仍然没有多大意义。 It simply unifies anything with the empty list: 它只是将所有内容与空白列表统一起来:

?- likes_somebody(chicken_tacos, []).
true.

You really need a predicate to tell you if someone is a girl, like this: 您确实需要一个谓词来告诉您某人是否是女孩,例如:

girl(dana).
girl(cody).
girl(bess).
girl(abby).

Then you could do what I think you're trying to do, which is something closer to this: 然后,您可以做我认为您想做的事情,这更接近于此:

likes_somebody(X) :- girl(X).

Then the maplist construction would work like this: 然后,maplist构造将像这样工作:

everybody(Everybody), maplist(likes_somebody, Everybody).

Which would simply return true. 这将简单地返回true。 You could simplify and eliminate everybody/1 by instead using findall(Girl, girl(X), Everybody) but it's getting weird. 您可以通过使用findall(Girl, girl(X), Everybody)来简化和消除findall(Girl, girl(X), Everybody) everybody/1 ,但它变得很奇怪。

You're trying to do list processing with likes_everybody/2 , but it's broken because girl is literally girl , not a variable, and others is literally others , not a list of some kind that could be the tail of another list. 你正在试图做的列表处理与likes_everybody/2 ,但因为它的破碎girl是字面上girl ,而不是一个变量, others是从字面上others ,而不是某种可能是另一个列表的尾部的名单。

I think you still have some old ideas you need to cleanse. 我认为您仍然需要清理一些旧的想法。 Read some more, write some more, and your code will start to make a lot more sense. 多读一些,多写一些,您的代码将变得更加有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM