我发现了一个3岁的问题,可以帮助我计算列表中变量的出现次数.问题的答案如下.代码有效.但是我无法理解,有人能帮我理解这个吗?
这是我发现的代码的答案,用引号写的是答案的一部分:
count([],X,0).
count([X|T],X,Y):- count(T,X,Z), Y is 1+Z.
count([X1|T],X,Z):- X1\=X,count(T,X,Z).
Run Code Online (Sandbox Code Playgroud)
'但请注意,第二个参数X应该被实例化.所以例如count([2,23,3,45,23,44,-20],23,C)将C与2统一.如果你想要每个元素的计数使用'
:- use_module(library(lists)).
count([],X,0).
count([X|T],X,Y):- count(T,X,Z), Y is 1+Z.
count([X1|T],X,Z):- X1\=X,count(T,X,Z)
countall(List,X,C) :-
sort(List,List1),
member(X,List1),
count(List,X,C).
Run Code Online (Sandbox Code Playgroud)
'那你得到'
?- countall([2,23,3,45,23,44,-20],X,Y).
X = -20,
Y = 1 ;
X = 2,
Y = 1 ;
X = 3,
Y = 1 ;
X = 23,
Y = 2 ;
X = 44,
Y = 1 ;
X = 45,
Y = 1 ;
no
Run Code Online (Sandbox Code Playgroud)
我是Prolog的新手,我只理解这段代码的一部分,就是这个
sort(List,List1),
member(X,List1),
Run Code Online (Sandbox Code Playgroud)
我会很感激这个问题的解释,特别是如何打印Y.
我坚持以下Prolog问题:给定图形的边缘没有周期作为事实.例如:
edge(a, b).
edge(b, c).
edge(c, d).
edge(c, e).
...
Run Code Online (Sandbox Code Playgroud)
我必须编写一个谓词来测试顶点X和Y之间是否有两条不同的路径.例如,two_paths(a, c).如果从节点a到节点c有两条不同的路径,则调用应该返回true.我知道如何检查两个顶点之间是否有路径:
path(X, Y) :- edge(X, Y).
path(X, Y) :- edge(X, Z), path(Z, Y).
Run Code Online (Sandbox Code Playgroud)
但是我该怎么做才能检查两条不同的路径呢?非常感谢您的帮助.
对于家庭作业,所以没有明确的,请:
有没有办法让Prolog只返回程序找到的第一个目标而忽略了找到的其他目标?
为说明目的,给定程序:
permutation([X|Xs],Zs):-permutation(Xs,Ys), insert(X,Ys,Zs).
permutation([],[]).
Run Code Online (Sandbox Code Playgroud)
有没有办法让程序只返回第一个排列作为唯一的解决方案?在以下情况中:
| ?- permutation([1,2,3],X).
X = [1,2,3] ? ;
X = [1,3,2] ? ;
X = [2,1,3] ? ;
X = [2,3,1] ? ;
X = [3,1,2] ? ;
X = [3,2,1] ? ;
no
Run Code Online (Sandbox Code Playgroud)
我们可以吗?
X = [1,2,3] ?;
no
Run Code Online (Sandbox Code Playgroud)
作为解决方案?
我被告知要创建一个兄弟谓词,找出兄弟是否有兄弟姐妹.Brother(B, S) :-.我知道你需要知道他们是否有同样的父母,但我不知道该怎么做.
father(dan,phil).
father(dan,jack).
father(dan,christine).
father(phil,kenton).
father(phil,shula).
father(phil,david).
father(phil,elizabeth).
father(jack,lillian).
father(jack,tony).
father(jack,jenny).
father(tony,tommy).
father(tony,helen).
father(tony,john).
father(roger,adam).
father(roger,debbie).
father(brian,kate).
father(david,pip).
father(david,josh).
father(mark,daniel).
mother(doris,phil).
mother(doris,jack).
mother(doris,christine).
mother(jill,kenton).
mother(jill,shula).
mother(jill,david).
mother(jill,elizabeth).
mother(peggy,lillian).
mother(peggy,tony).
mother(peggy,jenny).
mother(pat,tommy).
mother(pat,helen).
mother(pat,john).
mother(jenny,adam).
mother(jenny,debbie).
mother(jenny,kate).
mother(ruth,pip).
mother(ruth,josh).
mother(shula,daniel).
male(dan).
male(phil).
male(jack).
male(kenton).
male(david).
male(tony).
male(brian).
male(roger).
male(mark).
male(john).
male(tommy).
male(adam).
male(daniel).
male(josh).
female(doris).
female(jill).
female(peggy).
female(christine).
female(shula).
female(ruth).
female(elizabeth).
female(lillian).
female(pat).
female(jenny).
female(helen).
female(debbie).
female(kate).
female(pip).
dead(dan).
dead(doris).
dead(jack).
dead(mark).
dead(john).
dead(fred).
parent(X,Y) :-
father(X,Y); …Run Code Online (Sandbox Code Playgroud)