failure-slice

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.

Prevent backtracking after first solution to Fibonacci pair

天大地大妈咪最大 提交于 2019-12-18 13:36:31
问题 The term fib(N,F) is true when F is the N th Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, N2 #= N - 2, F1 #=< F, F2 #=< F, F #= F1 + F2, fib(N1,F1), fib(N2,F2). When executing this query (in SICStus Prolog), the first (and correct) match is found for N (rather instantly): | ?- fib(X,377). X = 14 ? When proceeding (by entering ";") to see if there are

Splitting a single list into three in order using Prolog

断了今生、忘了曾经 提交于 2019-12-13 13:09:02
问题 I am trying to make a function that splits a list of variable length into three lists of even length in order. The following splits it into three, but processes inserts them into each list one at a time. An example of what I want is: [1, 2, 3, 4, 5] -> [1, 2], [3, 4], [5] Another example would be: [8, 7, 6, 5, 4, 3, 2, 1] -> [8, 7, 6], [5, 4, 3], [2, 1]. The following code splits them by inserting into each list one at a time: div([], [], [], []). div([X], [X], [], []). div([X,Y], [X], [Y], [

DCG and left recursion

天大地大妈咪最大 提交于 2019-12-10 22:55:40
问题 I am trying to implement a dcg that takes a set of strings of the form {a,b,c,d}*.The problem i have is if I have a query of the form s([a,c,b],[]),It returns true which is the right answer but when i have a query of the form s([a,c,f],[]),It does not return an answer and it runs out of local stack. s --> []. s --> s,num. num --> [a]. num--> [b]. num--> [c]. num--> [d]. 回答1: Use phrase/2 Let's try phrase(s,[a,b,c]) in place of s([a,b,c],[]) . The reason is very simple: In this manner we are

An infinite success tree, or not?

最后都变了- 提交于 2019-12-10 20:33:20
问题 I'm given the following program: edge(a,b). edge(b,c). edge(a,d). path(N,M):- path(N,New),edge(New,M). path(N,M):- edge(N,M). And asked if when applying a proof tree algorithm to the following query: ?- path(a,X). the proof tree is an infinite success tree, or an infinite failure tree? Now as I see it, during the building of the tree, you get stuck in applying rule 1 of path over and over again, creating an infinite tree and never getting to rule 2 of path.. thus creating an infinite failure

Transforming a sentence creates an infinite loop - but how?

折月煮酒 提交于 2019-12-10 18:09:31
问题 I can't figure out where this is going wrong. Please note that I am very new to Prolog and I'm sure I'm missing something - just no idea what that might be. Could anyone help me out please? Thanks, here is my code: printSentence([]). printSentence([W|[]]) :- write(W), write('.'), nl. printSentence([W|R]) :- write(W), write(' '), printSentence(R). transform([], Result). transform([Word|Rest], Result) :- replace(Word, Replacement), append(Result, Replacement, NewResult), transform(Rest,

Prolog - How do I get the tail to not be null

徘徊边缘 提交于 2019-12-10 17:57:33
问题 I have the following problem: Define a predicate sorted(LL) , that is satisfied when the list LL contains other lists that are sorted in order of increasing length. For example: ?- sorted([[],[1],[1,1],[1,1,1]]) -> yes. ?- sorted([[],[1],[1,1]]) -> yes. ?- sorted([[1],[],[1,1],[1,1,1]]) -> no. And I have this code so far: % shorter/2 shorter([],_). shorter([_|T1], [_|T2]) :- shorter(T1,T2). % sorted/1 sorted([]). sorted([_]). sorted([L1,L2 | T]) :- shorter2(L1, L2), sorted([L2,T]). The

Stuck on an infinite loop in prolog

核能气质少年 提交于 2019-12-10 17:38:04
问题 I'd like my program to find me all sub-sets of size K of the integers 1,2,...,N. For this, I wrote the following subs(N,X,Y) means that X is a sub-set of size N of the set Y. I defined the following: subs(0,[],X). subs(N,[A|R1],[A|R2]):-N>0, N1 is N-1, subs(N1,R1,R2). subs(N,[A|R1],[B|R2]):-subs(N,[A|R1],R2). subs(N,[A|R1],[B|R2]):-subs(N,R1,[B|R2]). And then as a check I ran subs(2,X,[1,2,3,4]). I got the first answer [1,2], but never did it give a second answer, as it got stuck in an

Explanation of Prolog recursive procedure

こ雲淡風輕ζ 提交于 2019-12-10 16:44:59
问题 I'd like someone to explain this procedure if possible (from the book 'learn prolog now'). It takes two numerals and adds them together. add(0,Y,Y). add(s(X),Y,s(Z)) :- add(X,Y,Z). In principle I understand, but I have a few issues. Lets say I issue the query ?- add(s(s(0)), s(0), R). Which results in: R = s(s(s(0))). Step 1 is the match with rule 2. Now X becomes s(0) and Y is still s(0). However Z (according to the book) becomes s(_G648), or s() with an uninstantiated variable inside it.

Backtracking in recursive predicates

不想你离开。 提交于 2019-12-10 02:28:18
问题 Assume we have the following predicates (This is an example from Programming in Prolog): [F0] isInteger(0). [F1] isInteger(X):- isInteger(Y), X is Y+1. The first result for query isInteger(R), the marker is placed at F0, and will return R=0 If user presses ; , the marker is placed at F1, we move to subgoal(isInteger(Y), which is satisfied with F0) and R=1. I understand the above. Now here are my questions: If user presses ; again, where is the marker? How does the search proceed to return R=2