prolog

Prolog separating into two lists issue

*爱你&永不变心* 提交于 2019-12-17 21:21:07
问题 I have a prolog assignment. I need to look at the first item in a list, see if its following items are the same until they are not and separate the lists by the first item and its duplicates. e.g if my list was a,a,a,b,c it would separate it into first: a,a,a. second: b,c. My current solution works except that the final matching item comes into the second list, not the first. I can't seem to think of a way to get it to appear in the first list instead. grab([],[],[]). grab([A,A|L],[A|L2],Rest

Einstein Riddle with List of terms

旧街凉风 提交于 2019-12-17 21:11:20
问题 I implemented Einstein Riddle in Prolog and I'm trying to find out who had a fish at home. I can't find fault in this code and trace option is not helping with this problem ;) Rules: Norwegian lives in first house The Englishman lives in a red house. The green house is located directly on the left side of the white house. Dane drink tea. Light smoker lives next to the breeders of cats. A resident of the yellow house smokes a cigar. German smokes a water-pipe. A resident of the center house

Divison and remainder in Prolog

人走茶凉 提交于 2019-12-17 21:08:20
问题 Trying to figure out how to write a recursive predicate divide_by(X, D, I, R) that takes as input a positive integer X and a divisor D, and returns the answer as the whole number part I and the remainder part R, however, I can't seem to get my head around Prolog. How would I go about doing this? 回答1: There are predefined evaluable functors for this. (div)/2 and (mod)/2 always rounding down. Recommended by LIA-1, Knuth etc. (//)/2 and (rem)/2 rounding toward zero (actually, it's implementation

Pairwise relation over list

天涯浪子 提交于 2019-12-17 21:01:35
问题 The following higher order predicate succeeds if all pairs of the list's elements are true for a given relation. Is there a common or better, more intention revealing name for this relation? My original motivation for this name was that in clpfd, there is often a constraint all_different/1 which is described as being true, iff the elements are pairwise different . In fact, rather preferred to say the elements are all different, but I have been frequently corrected (by fellow Prolog

Prolog recursion grammar

我是研究僧i 提交于 2019-12-17 20:37:18
问题 at the moment I am having a problem with looping back to noun_phrase from np2 . I was wondering if someone can help me loop back to noun_phrase . Here is some code: noun_phrase([X|T],(det(X), NP2),Rem):- det(X), np2(T,NP2,Rem). np2([H|T],np2(adj(H),Rest),NP) :- adj(H), np2(T,Rest,Rem), noun_phrase(NP,Rem,_). I want to loop from np2 back to noun_phrase . I think the code for np2 is wrong as I just hacked it together. 回答1: Encoding a grammar directly in Prolog is a quite cumbersome process. Yes

Define graph in Prolog: edge and path, finding if there is a path between two vertices

本秂侑毒 提交于 2019-12-17 20:16:55
问题 I'm very new to Prolog. I defined in graph.pl the following graph: And here's my Prolog code: edge(a,e). edge(e,d). edge(d,c). edge(c,b). edge(b,a). edge(d,a). edge(e,c). edge(f,b). path(X,X). path(X,Y):- edge(X,Z) ; path(Z,Y). I understand it like this: there is a path between vertex X and vertex Y only if there is an edge between vertex X and vertex Z AND there is a path between vertex Z and vertex Y (some kind of recursion). Is that right for the presented graph? When I ask Prolog about

DCG Expansion: Is Steadfastness ignored?

自古美人都是妖i 提交于 2019-12-17 19:59:54
问题 Assume I have the following DCG rule: factor(X) --> "(", expr(X), ")". Normally this would be translated to: factor(X, A, B) :- [40|C] = A, expr(X, C, D), [41|B] = D. Would a Prolog system be allowed to translate it as follows, i.e. to merge the unifications into the head and the goal? factor(X, [40|A], B) :- expr(X, A, [41|B]). If DCG expansion would not be steadfast, it wouldn't be allowed to put [41|B] in the third argument of the expr call. But I guess steadfastness is in place, so

A searchable Prolog language description online [closed]

久未见 提交于 2019-12-17 19:17:02
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Is there a description of Prolog language (syntax and semantics) available online? There are a lot of reference manuals for implementations. But neither of those is a language description. For example the SWI Prolog manual states This manual does not describe the full syntax and semantics of Prolog. And refers

How do you do a search and replace of a list with another sublist in Prolog?

风流意气都作罢 提交于 2019-12-17 17:03:25
问题 I'm trying to modify a list by search and replace, was wondering how do I search through a list with the search term as a list as well? Lets say I have a list [1,2,3,4] I want to single out the 2 and 3 and replace it with 5,6 so ideally I could have a predicate: search_and_replace(Search_Term, Replace_Term, Target_List, Result_List). eg. search_and_replace([2,3], [5,6], [1,2,3,4], Result_List), write(Result_List). 回答1: You can use append/2 as follows : replace(ToReplace, ToInsert, List,

Simple Prolog delete from list

匆匆过客 提交于 2019-12-17 17:00:59
问题 (This is NOT a coursework question. Just my own personal learning.) I'm trying to do an exercise in Prolog to delete elements from a list. Here's my code : deleteall([],X,[]). deleteall([H|T],X,Result) :- H==X, deleteall(T,X,Result). deleteall([H|T],X,[H|Result]) :- deleteall(T,X,Result). When I test it, I first get a good answer (ie. with all the Xs removed.) But then the backtracking offers me all the other variants of the list with some or none of the instances of X removed. Why should