Edit Prolog program to output the result of a computation - prolog

I've found something about this in other questions, but mine is a bit different.
Given a string, I have to output another string with no adjacent duplicates.
E.g., given [a,a,b,b,c,d,a], my output will be [a,b,c,d,a].
Now, I've wrote the following recursive program to check if a certain given string has adjacent duplicates:
notequal(A,[]).
notequal(A,[X|S]) :- not(A=X).
noadj([]):-!.
noadj([A|S]) :- notequal(A,S), noadj(S).
How would I modify it in order to output what I described? I've tried multiple times but I'm new to prolog and I can't seem to get into its logic.
Of course, I'll need another variable, which would contain an element if notequal is true for that element.
So my idea is to iterate through the list and only add a certain term to the result if it passes the "notequal" test.
I'll edit this: I finally managed to do something like that by adding
noadjlist([X|S],[X|LS]) :- notequal(X,S), noadjlist(S,LS).
noadjlist([X|S],LS) :- noadjlist(S,LS).
noadjlist([],LS):-!.
However, my results are like:
?- noadjlist([1,2,2,3],LS).
LS = [1, 2, 3|_19316] .
why do I get that uninstantiated variable at the end?

noadjlist([],LS):-!.
should be
noadjlist([],[]):-!.

Related

print list of variable names in prolog

I'm using the swi-prolog build in predicate to get a list of variables from a term.
I am then trying to print them out with their sequence number.
Unfortunately I'm only able to print their internal _2902 names instead of X,Y, etc.
Is there a way to preserve the names? preferably not implementation dependant.
Here is what I have:
assign_numbers(Term) :-
term_variables(Term, L),
print_values(L,0).
% take a list of variables and print them out with their index according to their appearance order.
print_values([],_).
print_values([X|Xs],C) :-
C1 is C + 1,
write(X),tab(1),write(=),tab(1),write(C1),nl,
print_values(Xs,C1).
As a side question I would also like to see the implementation of the build in predicate term_variables from swi-prolog but I cant find it.

Graph implementation (does graph include all vertices?)

Trying to implement following predicate, which takes on input
list of lists - one list = one tested graph T (as edges)
graph G itself (as edges)
and tests whether T includes all of the vertices of G. If it does(true) should return it. Basically it's list filtering.
What I have so far is:
covb([],G).
covb([H|R],G) :-
isItCov(G,H), covb(R,G).
isItCov([],H).
isItCov([V-W|R],H) :-
vertex(V,H),
vertex(W,H),
isItCov(R,H).
vertex(V,[V-_|G]).
vertex(V,[_-V|G]).
vertex(V,[_|G]):- vertex(V,G).
For covb([[a-c,c-b,c-d]],[a-b,a-c,a-d,c-d,b-c]) works fine(true). For covb([[a-c]],[a-b,a-c,a-d,c-d,b-c]) works fine too (false). I got an issue while I call it with multiple lists like covb([[a-c,c-b,c-d],[a-c]],[a-b,a-c,a-d,c-d,b-c]). which should work only for the first one.
I actually got two questions -
Why does it work for one list only?
I want to return items of lists of lists which passed the condition and returned true (that's the filtering part). How should I do that?
First of all, your program has a number of singleton variable warnings. Do not ignore singleton variable warnings. They can hide real bugs. Also, since more experienced Prolog users here know that programs with singleton variable warnings are not even worth running, they will (a) just see the warnings and decide that they are no longer interested in trying to help you, or (b) fix the warnings on their side, but then by definition they will be working on a program that is no longer the program you posted!
Now for your questions.
Why does it work for one list only?
It's really not clear what you are asking here, or just above with "covb([[a-c,c-b,c-d],[a-c]],[a-b,a-c,a-d,c-d,b-c]). which should work only for the first one."
This query does fail:
?- covb([[a-c,c-b,c-d],[a-c]],[a-b,a-c,a-d,c-d,b-c]).
false.
This comes down to testing each of the two lists:
?- isItCov([a-b,a-c,a-d,c-d,b-c], [a-c,c-b,c-d]).
true .
?- isItCov([a-b,a-c,a-d,c-d,b-c], [a-c]).
false.
The first list does cover the graph, while the second doesn't. Overall, your definition of covb/2 is written to succeed if all lists cover the graph. This is not the case, so your covb/2 query fails.
Was this what you wanted to know?
I want to return items of lists of lists which passed the condition and returned true (that's the filtering part). How should I do that?
You could see if your Prolog's documentation has something for the word "filter". On SWI-Prolog you can do this:
?- apropos(filter).
true.
This will point you to the include/3 predicate, which seems to do what you want:
?- include(isItCov([a-b,a-c,a-d,c-d,b-c]), [[a-c,c-b,c-d],[a-c]], Covers).
Covers = [[a-c, c-b, c-d]].
If you want to write a filter predicate for your concrete application, it might look something like this:
graph_covers(_Graph, [], []).
graph_covers(Graph, [Nodes|NodesRest], Covers) :-
( isItCov(Graph, Nodes)
-> Covers = [Nodes|CoversRest]
; Covers = CoversRest ),
graph_covers(Graph, NodesRest, CoversRest).
This is similar to your predicate, it just adds an extra argument to collect those node lists for which isItCov/2 succeeded. If it did not succeed, it continues with a list not containing that current node list.
?- graph_covers([a-b,a-c,a-d,c-d,b-c], [[a-c,c-b,c-d],[a-c]], Covers).
Covers = [[a-c, c-b, c-d]] ;
false.

How do i count words in prolog?

I try to count words in a string in prolog. Like "No, I am definitely not a pie!"
To give me the number 7 and next example "w0w such t3xt... to give number 5.
I had thougt about subtract that are a library function and only get back white-characters. But the problem then is No way will give back 5 and not two words.
I thought about
filter([],L).
filter([X,Y|XS],[Y|XS]):- X = ' ',Y = ' ',L = [Y|XS], filter([Y|XS],L).
filter([X|XS],L):- filter(Xs,L).
That will remove white spaces and get back No way but it dosent work anbody have a tip.
Strings in Prolog are lists of character codes not of atoms, what explains why tests like X=' ' fail. See what is the result of executing
write("ab cd"), nl.
in your Prolog system.
You have errors in your 3 clauses:
What to do you expect the first clause to return in the last argument?
L is, as any other variable in a Prolog program, a variable that is local to the clause it appears in, never a global variable.
The second clause unifies L with a list and you use it as second argument of the recursive call: do you expect the recursive call to change the value of L? This will never be the case: in Prolog there is no assignment of variables, changes are made by building terms and unifying them with new variables.
What happens to X in your third clause???

Replace a list in prolog

I want to add a element to a list and replace it with my list.
So i wrote
initialstate(0,[],[[1,0],[2,3],[1,2],[1,3]]).
add(X,L2,[X|L2]).
I would like to make something like that...
?- initialstate(_,List,_),add(4,List,List)
and replace me the initialstate to this one
initial_state(0,[4],[[1,0],[2,3],[1,2],[1,3]]).
The maining question for me it's how to replace the list inside the indetifiers "initialstate".
I am new in prolog...Please help me... and sorry if it is something stupid.
once provided the appropriate declaration
:- dynamic initialstate/3.
you could update your DB in this way
?- retract(initialstate(A,B,C)), assertz(initialstate(A,[4|B],C)).
If you want to change a list you get a different list. So you need a different name for it.
Prolog is not an imperative language but more a descriptive one. This means: you do not say do this and this. You say this is like that and when this is the case this is true/false in files (knowledge). Then you start the interpreter, consult the knowledge and ask questions wether something is true.
Writing A = B means that A is always B.
?- initialstate(_,List,_),add(4,List,List)
means initialstate of a List is when 4 added to the list is the list itself.
This is not true. if 4 is added there should be a 4 where no 4 was before.
You can do this:
append4(List, ListWithFourInside) :- append(List, [4], ListWithFourInside).
Which reads like when I append 4 to a list I get a list with 4 inside.
It can be done shorter if you want the 4 in the beginning
add4(List, ListWithFourInside) :- [4 | List] = ListWithFourInside.
or even shorter:
add4(List, [4|List]).

Error in PROLOG code

I am new to prolog.
I want my code in PROLOG to produce the expected output given below. Can some one please tell me where I am going wrong.
The code is basically to remove duplicates and produce o/p in required format.
remove_dups([],_L2,_L2).
remove_dups([A|B],L2,L3) :-
functor(A,Pr,Ar),(member(level(Pr,Ar,1) ,L2) -> remove_dups(B,L2,L2); append([level(Pr,Ar,1)],L2,L3),remove_dups(B,L3,L3)).
expected output:
?- remove_dups([a,b,a],[],L).
L = [level(a,0,1),level(b,0,1)].
For starters I would have preferred to separate the two steps: removal of duplicates and presentation of the levels.
remove_dups([],[]).
remove_dups([X|Xs],Ys) :- member(X,Xs), !, remove_dups(Xs,Ys).
remove_dups([X|Xs],[X|Ys]) :- remove_dups(Xs,Ys).
levels([],[]).
levels([X|Xs],[level(N,A,1)|Ys]):- functor(X,N,A), levels(Xs,Ys).
go(L,R):- remove_dups(L,RL), levels(RL,R).
I have to admit that the constant 1 in the level tripples puzzles me. Are you sure that it should not be somehow more meaningful?
I have also assumed that the order of the list elements is of no importance: remove_dups removes all occurrences of a duplicated element except for the last one. If you would like to keep the first occurrence, remove_dups has to be modified.

Resources