prolog-dif

Remove unique elements only

为君一笑 提交于 2019-12-01 19:08:25
There are many resources on how to remove duplicates and similar issues but I can't seem to be able to find any on removing unique elements. I'm using SWI-Prolog but I don't want to use built-ins to achieve this. That is, calling remove_unique([1, 2, 2, 3, 4, 5, 7, 6, 7], X). should happily result in X = [2, 2, 7, 7] . The obvious solution is as something along the lines of count(_, [], 0) :- !. count(E, [E | Es], A) :- S is A + 1, count(E, Es, S). count(E, [_ | Es], A) :- count(E, Es, A). is_unique(E, Xs) :- count(E, Xs, 1). remove_unique(L, R) :- remove_unique(L, L, R). remove_unique([], _,

How does recursion in Prolog works from inside. One example

寵の児 提交于 2019-12-01 18:23:20
I've got here a small script, that converts list of elements into a set. For example list [1,1,2,3] -> set [1,2,3]. Can somebody explain to me, step by step, whats happening inside those procedures? Can you please use my [1,1,2,3] -> [1,2,3] example? list_to_set([],[]). list_to_set([A|X],[A|Y]):- list_to_set(X,Y), \+member(A,Y). list_to_set([A|X],Y):- list_to_set(X,Y), member(A,Y). The procedural counterpart of a prolog program is called SLDNF. Query: list_to_set([1,1,2,3], Result) Now SLDNF tries to match [1,1,2,3] with [A|X] through a procedure called unification. In a few words, it checks

Why would Prolog match a variable to a result that fails if plugged in directly?

喜你入骨 提交于 2019-12-01 06:11:05
I'm making a Prolog program that finds a subset of a set of lists. This subset must match some specific conditions, an aspect of which is that the subset's lists cannot be identical. What's confusing me is that when I try to find a match for a variable, X, it generates results that return false if I plug them into the query in place of X. For example: ?- containsSet(2, [[3],[7],[7]], X). X = [[3], [7]] ; X = [[3], [7]] ; X = [[7], [7]] ; false. ?- containsSet(2, [[3],[7],[7]], [[7],[7]]). false. How could it possibly match X to [[7], [7]] if, when plugged in directly, it returns false? The

Why would Prolog match a variable to a result that fails if plugged in directly?

♀尐吖头ヾ 提交于 2019-12-01 05:04:15
问题 I'm making a Prolog program that finds a subset of a set of lists. This subset must match some specific conditions, an aspect of which is that the subset's lists cannot be identical. What's confusing me is that when I try to find a match for a variable, X, it generates results that return false if I plug them into the query in place of X. For example: ?- containsSet(2, [[3],[7],[7]], X). X = [[3], [7]] ; X = [[3], [7]] ; X = [[7], [7]] ; false. ?- containsSet(2, [[3],[7],[7]], [[7],[7]]).

How to access list permutations in prolog?

房东的猫 提交于 2019-11-30 10:21:01
I want to access list permutation and pass it as argument to other functions. This is the permutation code: takeout(X,[X|R],R). takeout(X,[F|R],[F|S]) :- takeout(X,R,S), write(S). perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W). perm([],[]). To start with, let's redefine your predicates so they don't do any unnecessary I/O: takeout(X,[X|R],R). takeout(X,[F |R],[F|S]) :- takeout(X,R,S). perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W). perm([],[]). Now you have what could be considered a "pure" permutation function: ?- perm([1,2,3], X). X = [1, 2, 3] ; X = [2, 1, 3] ; X = [2, 3, 1] ; X = [1, 3, 2] ; X =

Why do we use '!' in prolog

匆匆过客 提交于 2019-11-29 18:25:24
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. mat Beginners tend to use !/0 because they are not aware of its negative consequences. This is because most Prolog textbooks that are popular among beginners are quite bad and

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

寵の児 提交于 2019-11-29 15:31:19
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. false The solution works as far as the first argument is a ground list. In some other cases, it is incorrect: ?- count([E], a, 0). false. Here we ask How must the element E of a list of length 1

filter list into separate lists

你说的曾经没有我的故事 提交于 2019-11-29 15:22:15
I need to filter the list [#,d,e,#,f,g] such that I get the output as [[d,e],[f,g]] , I am stuck while creating a new list every time I encounter '#' is there a way to do this? I tried the code below, filterL([],List) :-[]. filterL([Head|Tail],X) :- ( Head \='#'-> append(X,Head,List), filterL(Tail,List) ; filterL(Tail,X) ). Your problem is not very well defined. Are empty sequences allowed or not? Shall [#] be related to [[],[]] (there is an empty sequence before and after) or [] ? You say it should be [] . So: list_splitbyhash(Xs, Xss) :- phrase(splitby(Xss,#), Xs). splitby([],_E) --> [].

Prolog List Plateau

陌路散爱 提交于 2019-11-29 15:03:11
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'm still pretty confused by the whole declarative thing, and having a lot of trouble switching out of

Deleting all occurrences of an element from a list

只谈情不闲聊 提交于 2019-11-29 06:50:18
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, [T|Xs], Y) :- delMember(X, Xs, Y2), append([T], Y2, Y). it fail in queries like: delMember(Y, [1,2,3,1,2