prolog

Include non-unique elements only

断了今生、忘了曾经 提交于 2019-12-23 20:53:10
问题 This question was asked but there are no answers: here. I read the comments and tried to implement in both ways, but there are more problems that I don't understand. I first tried the easy way that doesn't keep original order: list_repeated(L, Ds) :- msort(L, S), sorted_repeated(S, Ds). sorted_repeated([], []). sorted_repeated([X|Xs], Ds) :- first(Xs, X, Ds). first([], _, []). first([X|Xs], X, [X|Ds]) :- more(Xs, X, Ds). first([X|Xs], Y, Ds) :- dif(X, Y), first(Xs, X, Ds). more([], _, []).

Find adjacent members

断了今生、忘了曾经 提交于 2019-12-23 20:32:58
问题 I have to find if two members of a list are adjacent. The restriction is to use the append/3 predicate. So far I've done the below, it works if it's true, otherwise I get no answer, it's like it runs eternally. adjacent(X,Y,L):- append(L1,[X,Y],T1),append(T1,T2,L). 回答1: To see that your program will loop, it suffices to consider the following failure-slice: adjacent(X,Y,L):- append(L1,[X,Y],T1), false , append(T1,T2,L) . If this program will loop, then the original program will loop too. It

Prolog - Rules are correct, but not outputting the way it's supposed to?

喜欢而已 提交于 2019-12-23 20:18:18
问题 Clue Four guests (Colonel Mustard, Professor Plum, Miss Scarlett, Ms. Green) attend a dinner party at the home of Mr. Boddy. Suddenly, the lights go out! When they come back, Mr Boddy lies dead in the middle of the table. Everyone is a suspect. Upon further examination, the following facts come to light: Mr Boddy was having an affair with Ms. Green. Professor Plum is married to Ms. Green. Mr. Boddy was very rich. Colonel Mustard is very greedy. Miss Scarlett was also having an affair with Mr.

Consecutive elements in a list

匆匆过客 提交于 2019-12-23 20:18:10
问题 I'm blocking on a predicate to code in Prolog . I need to code that two predicates: If I call : u([a,b,c,d,e,f], X). it will give X=[a,b], X=[b,c], X=[c,d] ... If I call : v([a,b,c,d,e,f], X). it will give X=[a,b], X=[c,d], X=[e,f] ... Thanks a lot! 回答1: Although false's answer is more elegant, here is a solution more appropriate for beginners for your predicate u/2 . u([X,Y|_], [X,Y]). u([_|Tail], XY):- u(Tail,XY). The first rule says that [X,Y] represent two consecutive elements in a list

Prolog: Getting predicate solutions and asserting them as facts

狂风中的少年 提交于 2019-12-23 20:14:20
问题 I've got a specific problem but I'll just try and come up with a general question so other people might benefit from it too... I've got a predicate that returns many solutions i.e. X=5; X=2; X=7 and I want a predicate that gets each of these solutions and asserts them as Prolog facts then so I end up with three facts in this case e.g. fact(5) fact(2) fact(7) so calling fact(5) would be true but calling fact(8) would be false because we never asserted it because it wasn't a solution. But I don

Querying Prolog variables with JPL

感情迁移 提交于 2019-12-23 19:49:35
问题 I want to make a query to use Prolog in java through JPL, I read the documentation (http://www.swi-prolog.org/packages/jpl/java_api/getting_started.html) The prolog predicates are these: child_of(joe, ralf). child_of(mary, joe). child_of(steve, joe). child_of(steve, ralf). descendent_of(X, Y) :- child_of(X, Y). descendent_of(X, Y) :- child_of(Z, Y), descendent_of(X, Z). My code looks like this Variable X = new Variable(); Query q4 = new Query( "descendent_of", new Term[] {X,new Atom("joe")} )

Creating a saved state in SWI-Prolog

核能气质少年 提交于 2019-12-23 19:04:45
问题 I am trying to create a saved state from toplevel in Windows, but I keep getting this error: 1 ?- qsave_program('U:/boo64.prc'). % library(broadcast) compiled into broadcast 0.00 sec, 7,504 bytes % library(debug) compiled into prolog_debug 0.00 sec, 21,544 bytes % library(option) compiled into swi_option 0.00 sec, 14,416 bytes % library(arithmetic) compiled into arithmetic 0.00 sec, 33,872 bytes % library(settings) compiled into settings 0.00 sec, 120,152 bytes % c:/program files/swi-prolog

prolog trace how to use

浪子不回头ぞ 提交于 2019-12-23 19:01:41
问题 How to go second step when trace prolog program? For example, I want to trace following simple program: length1([],0). length1([_X|Xs],N):- length1(Xs,N1), N is N1+1. I trace program: ?- trace,length([1,2,3],N). Call: (7) length([1, 2, 3], _G231) ? Exit: (7) length([1, 2, 3], 3) ? creep N = 3. But as we see, it immediately gives answer. But I thought it should be like Call:(8) ... Call:(9) ... What am I doing wrong? 回答1: Looking at your goal, you used the built-in length/2 , not your own

Can't understand why is prolog looping infinitly

只谈情不闲聊 提交于 2019-12-23 18:53:28
问题 From Bratko's book, Prolog Programming for Artificial Intelligence (4th Edition) We have the following code which doesn't work - anc4(X,Z):- anc4(X,Y), parent(Y,Z). anc4(X,Z):- parent(X,Z). In the book, on page 55, figure 2.15, is shown that parent(Y,Z) is kept calling until stack is out of memory. What I don't understand is that prolog does a recursiv call to anc4(X,Y), and not to parent (Y,Z) first. Why doesn't prolog goes over and over to the first line , anc4(X,Y) , and rather goes to the

Problems with using code program

风格不统一 提交于 2019-12-23 18:39:51
问题 Does it work on your machines? I don't know how to use it- i get errors every time. Tell me please how to use it.... Link to source: http://ai-programming.com/prolog_bot_tutorial.htm Code: % Program Name: chatterbot1 % Description: this is a very basic example of a chatterbot program % % Author: Gonzales Cenelia % Date: 7 august 2009 % response_database([ ['I HEARD YOU!'], ['SO, YOU ARE TALKING TO ME.'], ['CONTINUE, IM LISTENING.'], ['VERY INTERESTING CONVERSATION.'], ['TELL ME MORE...']]).