Related
I am trying to remove duplicates from a list while keeping the rightmost occurrences. E.g.: [1,2,3,1,2] is transformed in [3,1,2]
It's one of my first tries in Prolog and I don't understand what am I doing wrong. It always returns false. This is my code:
%nrap(L:list,E:element,S:integer)
%L - the initial list, list of integers
%E - the element, integer
%S - the result, nrap of E in L, S integer
%flow model: (i,i,o),(i,i,i)
nrap([],_,0).
nrap([H|T],E,S):-
H=E,
nrap(T,E,S1),
S is S1+1.
nrap([H|T],E,S):-
H\=E,
nrap(T,E,S).
%transform(L:list,L2:list,R:list)
%L - the initial list, list of integers
%L2 - copy of the initial list
%R - the resulted list, without duplicates, list of integers
%flow model: (i,i,o),(i,i,i)
transform([],[],[]).
transform([H|T],L2,[H|R]):-
nrap(L2,H,S),
S=1,
transform(T,L2,R).
transform([H|T],L2,R):-
nrap(L2,H,S),
S>1,
transform(T,L2,R).
Shall I be pure or impure? Why even consider sacrificing logical-purity if we can save it easily!
Using memberd_t/3 and if_/3, we define list_rset/2 and its left "twin" list_lset/2:
list_rset([], []). % keep rightmost occurrences
list_rset([E|Es], Rs0) :-
if_(memberd_t(E, Es),
Rs0 = Rs,
Rs0 = [E|Rs]),
list_rset(Es, Rs).
list_lset([], []). % keep leftmost occurrences
list_lset([E|Es], Ls) :-
post_pre_lset(Es, [E], Ls). % uses internal auxilary predicate
post_pre_lset([], _, []).
post_pre_lset([E|Es], Pre, Ls0) :- % 2nd arg: look-behind accumulator
if_(memberd_t(E, Pre),
Ls0 = Ls,
Ls0 = [E|Ls]),
post_pre_lset(Es, [E|Pre], Ls).
Let's run some queries!
?- _Es = [1,2,3,1,2], list_lset(_Es, Ls), list_rset(_Es, Rs).
Ls = [1,2,3], Rs = [3,1,2]. % succeeds deterministically
In above query 1 precedes 2 both at the beginning and at the end of the list [1,2,3,1,2]. What if 1 precedes 2 at the beginning but follows it at the end (e.g., [1,2,3,2,1])?
?- _Es = [1,2,3,2,1], list_lset(_Es, Ls), list_rset(_Es, Rs).
Ls = [1,2,3], Rs = [3,2,1]. % succeeds deterministically
Next, we look at a more general list_rset/2 goal that uses a list containing variables only. Thanks to #PauloMoura for his suggestion!
?- Es = [A,B,C,A,B], list_rset(Es,Rs).
Es = [C,C,C,C,C], Rs = [ C], A=B , B=C
; Es = [B,B,C,B,B], Rs = [C, B], A=B , dif(B,C)
; Es = [C,B,C,C,B], Rs = [ C,B], A=C , dif(B,C)
; Es = [A,C,C,A,C], Rs = [ A,C], dif(A,C), B=C
; Es = [A,B,C,A,B], Rs = [C,A,B], dif(A,B), dif(A,C), dif(B,C).
What's up with the residual goals (above)?
Without sufficient instantiation, dif/2 is not decidable.
To save logical soundness, the execution of the prolog-dif constraints is delayed.
Last, one more use-case: an "input" list Xs that has both variables and ground terms.
?- Es = [A,B,z], list_rset(Es,Rs).
Es = [z,z,z], Rs = [ z], A=B , B=z
; Es = [B,B,z], Rs = [B, z], A=B , dif(B,z)
; Es = [z,B,z], Rs = [ B,z], A=z , dif(B,z)
; Es = [A,z,z], Rs = [A, z], dif(A,z), B=z
; Es = [A,B,z], Rs = [A,B,z], dif(A,B), dif(A,z), dif(B,z).
This is a follow-up to this previous answer... In this answer we use dcg!
We build lset//1 upon memberd_t/3 and if_//3—the dcg analogue of if_/3:
lset([]) -->
[].
lset([X|Xs]) -->
[X],
lset_pre(Xs,[X]).
lset_pre([],_) -->
[].
lset_pre([X|Xs],Pre) -->
if_(memberd_t(X,Pre), [], [X]),
lset_pre(Xs,[X|Pre]).
Same for rset//1:
rset([]) -->
[].
rset([X|Xs]) -->
if_(memberd_t(X,Xs), [], [X]),
rset(Xs).
Some sample queries:
?- _Es = [1,2,3,1,2], phrase(lset(_Es),Ls), phrase(rset(_Es),Rs).
Ls = [1,2,3], Rs = [3,1,2]. % succeeds deterministically
?- _Es = [1,2,3,2,1], phrase(lset(_Es),Ls), phrase(rset(_Es),Rs).
Ls = [1,2,3], Rs = [3,2,1]. % succeeds deterministically
This is easier than you are making it. Since the elements in the "set" have to be in the order of last appearance, you don't need to keep a copy of the list at all: just compare to the remainder of the list (the tail).
If you know that the first list is always going to be ground (all elements are integers, for example), you could write:
list_set([], []).
list_set([X|Xs], Ys0) :-
( memberchk(X, Xs)
-> Ys0 = Ys
; Ys0 = [X|Ys]
),
list_set(Xs, Ys).
memberchk/2 can be used to check if a ground term is in a list of ground terms. It will succeed or fail exactly once.
A more general solution is to pose a constraint that an element should be in the set if it is different from all the elements following it, and be dropped otherwise:
list_set([], []).
list_set([X|Xs], [X|Ys]) :-
maplist(dif(X), Xs),
list_set(Xs, Ys).
list_set([X|Xs], Ys) :-
\+ maplist(dif(X), Xs),
list_set(Xs, Ys).
Here, maplist(dif(X), Xs) means:
X is different from every element in the list Xs (the tail).
and \+ Goal succeeds then Goal does not succeed.
With this defintion:
?- list_set([1,2,3,1,2], S).
S = [3, 1, 2] ;
false.
?- list_set([1,2,3,3,1,1,2], S).
S = [3, 1, 2] ;
false.
?- list_set([A,B,C,A,B],Xs).
Xs = [C, A, B],
dif(A, B),
dif(C, B),
dif(C, A) ;
false.
I would solve it by comparing the first index of the first list and adding 2 to the index. But I do not know how to check for indexes in prolog.
Also, I would create a counter that ignores what is in the list when the counter is an odd number (if we start to count from 0).
Can you help me?
Example:
everyOther([1,2,3,4,5],[1,3,5]) is true, but everyOther([1,2,3,4,5],[1,2,3]) is not.
We present three logically-pure definitions even though you only need one—variatio delectat:)
Two mutually recursive predicates list_oddies/2 and skipHead_oddies/2:
list_oddies([],[]).
list_oddies([X|Xs],[X|Ys]) :-
skipHead_oddies(Xs,Ys).
skipHead_oddies([],[]).
skipHead_oddies([_|Xs],Ys) :-
list_oddies(Xs,Ys).
The recursive list_oddies/2 and the non-recursive list_headless/2:
list_oddies([],[]).
list_oddies([X|Xs0],[X|Ys]) :-
list_headless(Xs0,Xs),
list_oddies(Xs,Ys).
list_headless([],[]).
list_headless([_|Xs],Xs).
A "one-liner" which uses meta-predicate foldl/4 in combination with Prolog lambdas:
:- use_module(library(lambda)).
list_oddies(As,Bs) :-
foldl(\X^(I-L)^(J-R)^(J is -I,( J < 0 -> L = [X|R] ; L = R )),As,1-Bs,_-[]).
All three implementations avoid the creation of useless choicepoints, but they do it differently:
#1 and #2 use first-argument indexing.
#3 uses (->)/2 and (;)/2 in a logically safe way—using (<)/2 as the condition.
Let's have a look at the queries #WouterBeek gave in his answer!
?- list_oddies([],[]),
list_oddies([a],[a]),
list_oddies([a,b],[a]),
list_oddies([a,b,c],[a,c]),
list_oddies([a,b,c,d],[a,c]),
list_oddies([a,b,c,d,e],[a,c,e]),
list_oddies([a,b,c,d,e,f],[a,c,e]),
list_oddies([a,b,c,d,e,f,g],[a,c,e,g]),
list_oddies([a,b,c,d,e,f,g,h],[a,c,e,g]).
true. % all succeed deterministically
Thanks to logical-purity, we get logically sound answers—even with the most general query:
?- list_oddies(Xs,Ys).
Xs = [], Ys = []
; Xs = [_A], Ys = [_A]
; Xs = [_A,_B], Ys = [_A]
; Xs = [_A,_B,_C], Ys = [_A,_C]
; Xs = [_A,_B,_C,_D], Ys = [_A,_C]
; Xs = [_A,_B,_C,_D,_E], Ys = [_A,_C,_E]
; Xs = [_A,_B,_C,_D,_E,_F], Ys = [_A,_C,_E]
; Xs = [_A,_B,_C,_D,_E,_F,_G], Ys = [_A,_C,_E,_G]
; Xs = [_A,_B,_C,_D,_E,_F,_G,_H], Ys = [_A,_C,_E,_G]
...
There are two base cases and one recursive case:
From an empty list you cannot take any odd elements.
From a list of length 1 the only element it contains is an odd element.
For lists of length >2 we take the first element but not the second one; the rest of the list is handled in recursion.
The code looks as follows:
odd_ones([], []).
odd_ones([X], [X]):- !.
odd_ones([X,_|T1], [X|T2]):-
odd_ones(T1, T2).
Notice that in Prolog we do not need to maintain an explicit index that has to be incremented etc. We simply use matching: [] matches the empty list, [X] matches a singleton list, and [X,_|T] matches a list of length >2. The | separates the first two elements in the list from the rest of the list (called the "tail" of the list). _ denotes an unnamed variable; we are not interested in even elements.
Also notice the cut (!) which removes the idle choicepoint for the second base case.
Example of use:
?- odd_ones([], X).
X = [].
?- odd_ones([a], X).
X = [a].
?- odd_ones([a,b], X).
X = [a].
?- odd_ones([a,b,c], X).
X = [a, c].
?- odd_ones([a,b,c,d], X).
X = [a, c].
?- odd_ones([a,b,c,d,e], X).
X = [a, c, e].
I've created a function in Prolog to "turn" a list, e.g. to append the head of a list to the tail like so:
?- turn([a,b,c,d,e], Tlist).
Tlist=[b,c,d,e,a]
Within the context of my program, I'd like to be able to use predefined lists for the rule, such as
alist([a,b,c,d,e,f])
but I get lots of different errors. I've tried the following as arguments:
turn(alist(L),R).
listv(X) :- alist(L), member(X, L).
turn(listv(X),R).
and I understand that each of these are different representations of the list according to Prolog, but I'm not sure which list representation is appropriate to complete the operation on a predefined list.
Thanks!
The predicate turn/2 can easily be defined based on append/3:
turn([A|As],Bs) :-
append(As,[A],Bs).
For specifying some sample lists, we define an auxiliary predicate named_list/2:
named_list(one_to_nine , [1,2,3,4,5,6,7,8,9]).
named_list(a_to_f , [a,b,c,d,e,f]).
named_list(single_digit_primes, [2,3,5,7]).
Let's query!
?- named_list(a_to_f,Xs), turn(Xs,Ys).
Xs = [a,b,c,d,e,f], Ys = [b,c,d,e,f,a]. % succeeds deterministically
?- named_list(a_to_f,Ys), turn(Xs,Ys). % "other" direction
Ys = [a,b,c,d,e,f], Xs = [f,a,b,c,d,e] % succeeds leaving behind choicepoint
; false.
So far, we have used one specific sample list a_to_f in the queries; let's use 'em all!
?- named_list(Name,Xs), turn(Xs,Ys).
Name = one_to_nine , Xs = [1,2,3,4,5,6,7,8,9], Ys = [2,3,4,5,6,7,8,9,1]
; Name = a_to_f , Xs = [a,b,c,d,e,f] , Ys = [b,c,d,e,f,a]
; Name = single_digit_primes, Xs = [2,3,5,7] , Ys = [3,5,7,2].
I'm attempting to make a predicate that takes a list of pairs and, if it finds the key in the list it will remove that item from the list and return the rest. However, it also needs to return the full list if the key given does not exist.
unmap(K, M1, M2):-
select(E, M1, MM1),
select(E, [(K, _)], K1),
unmap(MM1, K1, M2).
unmap(X, _, X).
Called with:
unmap(key1, [(key1, value1),(key2, value2),(key3, value3)], R).
Results in:
R = [(key2, value2), (key3, value3)]
Works, but theres a problem. I'm trying to make it return the identical list thats given if the key1 does not exist. Here's what it returns:
Calling:
unmap(key4, [(key1, value1),(key2, value2),(key3, value3)], R).
Returns:
R = key4
I think it's something to do with my terminating rule, but I'm not sure how to go about fixing it. Thanks very much in advance for all that can help.
Of course, you can do it with logical-purity! Here's how...
Let's call the actual relation pairs_key_unmapped/3. That's a somewhat more descriptive name. unmap/3 is just a wrapper for pairs_key_unmapped/3:
unmap(Key,Ps0,Ps) :-
pairs_key_unmapped(Ps0,Key,Ps).
The implementation of pairs_key_unmapped/3 is built on the predicates if_/3 and =/3 (a.k.a. equal_truth/3), as defined by #false in an answer to "Prolog union for A U B U C":
pairs_key_unmapped([],_,[]).
pairs_key_unmapped([P|Ps],K,Us) :-
P = (K0,_),
if_(K0=K, Ps=Us, (Us=[P|Us0],pairs_key_unmapped(Ps,K,Us0))).
Let's run some queries!
?- unmap(key1,[(key1,value1),(key2,value2),(key3,value3)],Ps).
Ps = [(key2,value2),(key3,value3)]. % succeeds deterministically
?- unmap(key4,[(key1,value1),(key2,value2),(key3,value3)],Ps).
Ps = [(key1,value1),(key2,value2),(key3,value3)]. % succeeds deterministically
Let's try something different... What if Key occurs twice in Ps0?
?- unmap(key1,[(key1,x),(key1,y)],Ps). % only the 1st occurrence is removed
Ps = [(key1,y)]. % succeeds deterministically
What if Ps0 is unknown, but Ps is known?
?- unmap(key4,Ps0,[(key1,value1),(key2,value2),(key3,value3)]).
Ps0 = [(key4,_A), (key1,value1),(key2,value2),(key3,value3)] ;
Ps0 = [(key1,value1),(key4,_A), (key2,value2),(key3,value3)] ;
Ps0 = [(key1,value1),(key2,value2),(key4,_A), (key3,value3)] ;
Ps0 = [(key1,value1),(key2,value2),(key3,value3) ] ;
Ps0 = [(key1,value1),(key2,value2),(key3,value3),(key4,_A) ] ;
false.
How about something a little more general?
?- unmap(Key,Ps0,[_,_]).
Ps0 = [(Key,_A),_B, _C ] ;
Ps0 = [(_A,_B), (Key,_C), _D ], dif(_A,Key) ;
Ps0 = [(_A,_B), (_C,_D) ], dif(_A,Key), dif(_C,Key) ;
Ps0 = [(_A,_B), (_C,_D), (Key,_E)], dif(_A,Key), dif(_C,Key) ;
false.
And what answers does the most general query give us?
?- unmap(Key,Ps0,Ps).
Ps0 = [], Ps = [] ;
Ps0 = [(Key,_A)|Ps] ;
Ps0 = [(_A,_B)], Ps = [(_A,_B)], dif(_A,Key) ;
Ps0 = [(_A,_B),(Key,_C)|_Z], Ps = [(_A,_B)|_Z], dif(_A,Key) ;
Ps0 = [(_A,_B),(_C,_D)], Ps = [(_A,_B),(_C,_D)], dif(_A,Key), dif(_C,Key) ...
The issue is with your base case:
unmap(X, _, X).
If your main predicate clause fails (the key isn't found), it reverts to the base case, which will instantiate your result (third argument) with only the key (first argument). Your base case should be:
unmap(_, X, X).
Which will instantiate the result (third argument) with the original list (second argument).
Note that the main clause could be simpler (this will work in GNU or SWI prolog):
unmap(K, M, R):-
select((K, _), M, M1),
unmap(K, M1, R), !.
The cut prevents backtracking to the base case if the first clause succeeds.
In SWI Prolog, the delete/3 predicate will work in your favor:
unmap(K, M, R) :-
delete(M, (K,_), R), !.
delete/3 is more strict in GNU Prolog and will not work in this case.
This isn't so much an answer to the question, but a simpler way of attacking it, without using the 'select' (or any other built-in predicates), and only using recursion.
Considering that the output list is just a list of items that didn't match the key, you need 2 main clauses, and iterate around the list. One where the key matches the head of the list, and one that doesn't.
unmap(_, [], []).
% head of the list matches key, do not add K/H to unmatched list (ie remove it)
unmap(K, [(H, _)|Tail], Unmatched) :-
H == K,
unmap(K, Tail, Unmatched).
% above rule fails, add H to unmatched list
unmap(K, [H|Tail], [H|Unmatched]) :-
unmap(K, Tail, Unmatched).
?- unmap(key1, [(key1, value1),(key2, value2),(key3, value3)], R).
R = [ (key2, value2), (key3, value3)] .
?- unmap(key4, [(key1, value1),(key2, value2),(key3, value3)], R).
R = [ (key1, value1), (key2, value2), (key3, value3)] .
So if the key doesn't exist, it just iterates around adding all list items, and so the input and output lists are identical.
In Prolog I have to figure out a way how to combine two already sorted lists into one sorted list. So in other words: from 2 lists I have to compare the heads and add the smallest to a new list. I think I've gotten pretty far, but somehow it just doesn't work and I can't figure out why not. I don't get any errors BTW. It just gives false back.
So, I hope someone can tell me what I'm doing wrong.
sort([],L,L).
sort(L,[],L).
sort([Head1|Tail1],[Head2|Tail2],L) :-
Head1 < Head2 -> append([Head1],L,L2), sort(Tail1,[Head2|Tail2],L2) ;
Head1 > Head2 -> append([Head2],L,L2), sort([Head1|Tail1],Tail2,L2) ;
Head1 == Head2 -> append([Head1],L,L2), append([Head2],L2,L3),
sort(Tail1,Tail2,L3).
You should simplify your code a bit:
sort([],L,L).
sort(L,[],L).
sort([Head1|Tail1], [Head2|Tail2], L) :-
Head1 < Head2 -> L = [Head1|R], sort(Tail1,[Head2|Tail2],R) ;
Head1 > Head2 -> L = [Head2|R], sort([Head1|Tail1],Tail2,R) ;
L = [Head1,Head2|R], sort(Tail1,Tail2,R).
test:
?- sort([1,2,4,5,18],[1,3,5,10],R).
R = [1, 2, 3, 4, 5, 10, 18] .
Naming such predicate sort is really misleading, merge would be much better....
edit L = [Head1,Head2|R] instead of L = [Head1|R] where Head1=Head2 (having failed the previous tests) departs from sort/2 Prolog semantic, that removes duplicates.
In SWI Prolog there's merge/3 built-in predicate that does exactly that. So you shouldn't call your predicate "sort"; it's not. It's "merge".
Next, let's read your definition.
merge([H1|T1], [H2|T2], L) :-
means, merging the two lists produces the merged list L. So far, so good.
H1 < H2 -> append([H1],L,L2), merge(T1,[H2|T2],L2)
means, in case H1 < H2, prefixing the answer list L with H1 gives L2, which is the result of merging ... Wait, what? Does that make sense?
Prolog is not Basic. What we write are not "commands" to "do" stuff (well, they are, but in a roundabout way). If we want to say that H1 is the head element of L, we say it:
L = [H1|L2], merge(T1,[H2|T2],L2)
%% or: append([H1],L2,L),
etc. Now this makes sense. :)
Telling from the code in your question, you are merging sorted lists of numbers.
If these numbers are all integers and if your Prolog system offers clpfd, consider using code presented here. Why?
The implementation preserves logical-purity.
The code is monotone, making it robust and flexible: always get logically sound answers, even when working for non-ground data.
The main predicate sorted1_sorted2_merged/3 behaves like a real relation.
Unlike the builtin sort/2, this code keeps all duplicates (in exactly the same multiplicities).
Regarding equal items in the lists to be merged, it behaves reasonable:
Items in input #1 that are equal to some items in input #2 precede those in the merged result.
The implementation is efficient in the sense that it does not leave useless choicepoints with goals like sorted1_sorted2_merged([1,3,5],[2,4,5,6],Zs).
Without any further ado... here's the code:
:- use_module(library(clpfd)).
sorted1_sorted2_merged([] ,Ys,Ys).
sorted1_sorted2_merged([X|Xs],Ys,Zs) :-
sorted2_hd1_tl1_merged(Ys,X,Xs,Zs).
hd1_tl1_hd2_tl2_merged(X,Xs,Y,Ys,Zs) :-
zcompare(Op,X,Y),
op_hd1_tl1_hd2_tl2_merged(Op,X,Xs,Y,Ys,Zs).
sorted1_hd2_tl2_merged([] ,Y,Ys,[Y|Ys]).
sorted1_hd2_tl2_merged([X|Xs],Y,Ys,Zs) :-
hd1_tl1_hd2_tl2_merged(X,Xs,Y,Ys,Zs).
sorted2_hd1_tl1_merged([] ,X,Xs,[X|Xs]).
sorted2_hd1_tl1_merged([Y|Ys],X,Xs,Zs) :-
hd1_tl1_hd2_tl2_merged(X,Xs,Y,Ys,Zs).
op_hd1_tl1_hd2_tl2_merged(<,X,Xs,Y,Ys,[X|Zs]) :-
sorted1_hd2_tl2_merged(Xs,Y,Ys,Zs).
op_hd1_tl1_hd2_tl2_merged(=,X,Xs,Y,Ys,[X|Zs]) :-
sorted1_hd2_tl2_merged(Xs,Y,Ys,Zs).
op_hd1_tl1_hd2_tl2_merged(>,X,Xs,Y,Ys,[Y|Zs]) :-
sorted1_hd2_tl2_merged(Ys,X,Xs,Zs).
On to some queries! First:
?- sorted1_sorted2_merged([1,3,4,6],[2,4,5,5,7],Xs).
Xs = [1,2,3,4,4,5,5,6,7]. % succeeds deterministically
Does it work in the "other directions", too?
?- sorted1_sorted2_merged([1,3,4,6],Ys,[1,2,3,4,4,5,5,6,7]).
Ys = [2,4,5,5,7] ; % succeeds, but leaves behind choicepoint
false.
?- sorted1_sorted2_merged(Xs,[2,4,5,5,7],[1,2,3,4,4,5,5,6,7]).
Xs = [1,3,4,6] ; % succeeds, but leaves behind choicepoint
false.
At last, a quite general use:
?- sorted1_sorted2_merged(Xs,Ys,[0,1,2,3]).
Xs = [ ], Ys = [0,1,2,3] ;
Xs = [0,1,2,3], Ys = [ ] ;
Xs = [0 ], Ys = [ 1,2,3] ;
Xs = [0,1 ], Ys = [ 2,3] ;
Xs = [0,1,2 ], Ys = [ 3] ;
Xs = [0,1, 3], Ys = [ 2] ;
Xs = [0, 2,3], Ys = [ 1] ;
Xs = [0, 3], Ys = [ 1,2 ] ;
Xs = [0, 2 ], Ys = [ 1, 3] ;
Xs = [ 1,2,3], Ys = [0 ] ;
Xs = [ 2,3], Ys = [0,1 ] ;
Xs = [ 3], Ys = [0,1,2 ] ;
Xs = [ 2 ], Ys = [0,1, 3] ;
Xs = [ 1 ], Ys = [0, 2,3] ;
Xs = [ 1,2 ], Ys = [0, 3] ;
Xs = [ 1, 3], Ys = [0, 2 ] ;
false.