prolog-dif

Prolog separating into two lists issue

拈花ヽ惹草 提交于 2019-11-28 14:46:30
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) :- grab([A|L],L2,Rest). grab([A|L],Init,[A|L2]) :- grab(L,Init,L2). When the first two elements are

Why do we use '!' in prolog

爷,独闯天下 提交于 2019-11-28 14:16:53
问题 This is the code that i am trying to understand. co(X) :- co(X,[],L). co([],A,A):- write(A). co([X|Xs], A, L) :- p(X-Z,A,R), !, Z1 is Z+1, co(Xs, [X-Z1|R], L). co([X|Xs], A, L) :- co(Xs, [X-1|A], L). p(X-Y,[X-Y|R],R):- !. p(X,[H|Y], [H|Z]) :- p(X,Y,Z). What is the use of '!' and predicate p(,,) in the above code. OR Can anybody just add comments in every step of the above code so that i can able to understand . Thanks. 回答1: Beginners tend to use !/0 because they are not aware of its negative

Force Prolog to choose unique values of variables

僤鯓⒐⒋嵵緔 提交于 2019-11-28 12:12:13
OK I am new to Prolog, so excuse me if this is something trivial, but I can't seem to find a proper elegant answer to this. I am trying to work out the exercise here on learnprolognow.org , exercise 2.4 (the crossword). The exercise provides these facts: word(astante, a,s,t,a,n,t,e). word(astoria, a,s,t,o,r,i,a). word(baratto, b,a,r,a,t,t,o). word(cobalto, c,o,b,a,l,t,o). word(pistola, p,i,s,t,o,l,a). word(statale, s,t,a,t,a,l,e). And the solution I came up with to solve the crossword placement of each word is this: crossword(V1, V2, V3, H1, H2, H3) :- word(V1, V1a, V1bH1b, V1c, V1dH2b, V1e,

How can I delete every occurrence of a sublist from a list in prolog?

依然范特西╮ 提交于 2019-11-28 11:44:56
问题 This is the code for deleting or removing an element from a given list: remove_elem(X,[],[]). remove_elem(X,L1,L2) :- L1 = [H|T], X == H, remove_elem(X,T,Temp), L2 = Temp. remove_elem(X,L1,L2) :- L1 = [H|T], X \== H, remove_elem(X,T,Temp), L2 = [H|Temp]. How can I modify it, so that I can delete every occurrence of a sub list from a list? When I tried to put a list in an element, it only deletes the element and only once. It should be this: ?- remove([1,2],[1,2,3,4,1,2,5,6,1,2,1],L). L = [3,4

I want to count the occurrences of an element in a list

戏子无情 提交于 2019-11-28 09:02:18
问题 I want to count the occurrences of an element in a list, and if there is one then the predicate unique would be true, else false. However, if the element occurs more than once, Prolog finds it true. I don't know what to do... count([], X, 0). count([X|T], X, Y) :- count(T, X, Z), Y is 1+Z, write(Z). count([_|T], X, Z) :- count(T, X, Z). unique(St, [Y|RestList]) :- count([Y|RestList], St, N), N =:= 1. 回答1: The solution works as far as the first argument is a ground list. In some other cases,

Prolog List Plateau

喜你入骨 提交于 2019-11-28 08:40:58
问题 Just got introduced to prolog, trying to get through some simple exercises, but I've been getting kind of stuck on this one. I'm trying to write a program that outputs all the sublists of the input list, where each sublist has length > 1 and it cannot be extended to a larger sublist. It will also output the starting position in the list of the sublist. So a sample output would be | ?- plateau([a,a,b,2,2,2,a+1,a+1,s(1,2)], I, Len). I = 1, Len = 2 ? ; I = 4, Len = 3 ? ; I = 7, Len = 2 ? ; no I

Simple Prolog delete from list

*爱你&永不变心* 提交于 2019-11-28 02:01:40
(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 this be? Why do cases where H==X ever fall through to the last clause? Aasmund Eldhuset The last clause

What are the pros and cons of using manual list iteration vs recursion through fail

可紊 提交于 2019-11-28 01:55:22
I come up against this all the time, and I'm never sure which way to attack it. Below are two methods for processing some season facts. What I'm trying to work out is whether to use method 1 or 2, and what are the pros and cons of each, especially large amounts of facts. methodone seems wasteful since the facts are available, why bother building a list of them (especially a large list). This must have memory implications too if the list is large enough ? And it doesn't take advantage of Prolog's natural backtracking feature. methodtwo takes advantage of backtracking to do the recursion for me,

different/2 - does a pure, determinate definition exist?

心已入冬 提交于 2019-11-28 01:47:11
different(Xs, Ys) :- member(X, Xs), non_member(X, Ys). different(Xs, Ys) :- member(Y, Ys), non_member(Y, Xs). While this definition using member/2 and non_member/2 is almost 1 perfect from a declarative viewpoint, it produces redundant solutions for certain queries and leaves choice points all around. What is a definition that improves upon this (in a pure manner probably using if_/3 and (=)/3 ) such that exactly the same set of solutions is described by different/2 but is determinate at least for ground queries (thus does not leave any useless choice points open) and omits (if possible) any

Deleting all occurrences of an element from a list

旧街凉风 提交于 2019-11-28 00:20:40
问题 Trying to write a procedure that given a value and a list, it deletes all the occurence of that value in the list a wrote: delMember(X, [], []) :- !. delMember(X, [X|Xs], Y) :- !, delMember(X, Xs, Y). delMember(X, [T|Xs], Y) :- !, delMember(X, Xs, Y2), append([T], Y2, Y). Since the cut this code cannot answer correctly queries like: delMember(Y, [1,2,3,1,2,3,1,2,3], [1, 2, 1, 2, 1, 2 ]). If I delete the cuts: delMember(X, [], []). delMember(X, [X|Xs], Y) :- delMember(X, Xs, Y). delMember(X,