Replicate list by another list's number of items - prolog

I'm trying to make this:
times([x, x], [1, 5, 9, 8], Result).
The second list is replicated by the number of elements in the first one.
The result is : [1, 5, 9, 8, 1, 5, 9, 8]
I've tried this but is not working properly:
times( [ ], _, [ ] ) :- !.
times( [ _ | List1 ], List2, Result ) :- append( [], List2, Result ),
times( List1, List2, Result ).
Thanks in advance!

Firstly, you do not need your cut in the base case. It unnecessarily prunes the solution search:
replicate([], _, []). % The empty list is the result of replicating any list by []
Then your recursive rule, replicate([_|List1], List2, Result) seems to have a reasonable format/head. But you have logic issues in the body. If you were to describe the logic, it would not make sense. In particular, you're using Result in two different places for two different meanings, and that will result in unexpected failure. The predicate just needs to be thought out logically for what it means:
Result is the replication of List2 by [_|List1] if SubResult is the replication of List2 by List1 and Result is the result of appending SubResult to List2.
Note the use of a "subresult" (SubResult) here which is distinguished from the main result (Result). It's important that these be different variables.
If you write that as Prolog, you get:
replicate([_|List1], List2, Result) :-
replicate(List1, List2, SubResult),
append(List2, SubResult, Result).
I didn't test this, but this should basically do it. You should try it and resolve any residual issues yourself as part of your Prolog learning process. I also did not consider whether there's a more effective approach to the overall problem but am just resolving your issues with your current approach.
Another would be to use maplist/2 and append/2. You can use maplist/3 to get a list of lists, then use append/2 to get your result:
replicate(List1, List2, Result) :-
length(List1, Len),
length(R1, Len),
maplist(=(List2), R1),
append(R1, Result).
With a little more thought, this can be solved using simple recursive list handling. In this case, you would recursively unify the head of the result with the head of each element in the second list, then continue this process for each element in the first list.
replicate(Rep, List, Res) :-
replicate(Rep, List, List, Res).
replicate([], _, _, []).
replicate([R|Rs], List, [X|Xs], [X|Res]) :-
replicate([R|Rs], List, Xs, Res).
replicate([R|Rs], List, [], Res) :-
replicate(Rs, List, List, Res).

We can literally substitute the second list for each element in the first, then call append/2 (SWI Prolog has one):
nreplicate( Xs, Ys, Result) :-
maplist( subst(Ys), Xs, Zs),
append( Zs, Result ).
subst(Ys, _, Ys).

Related

delete all occurences of a term in a list

I know this sounds "duplicate" but please help me out
I have defined three terms as follows:
type([a, b, c, d]:location).
type([coffee, tea, lemonade, water, biscuits]: object).
type([order(object, location)]: order).
I have a piece of code that then generates a list of random orders.
I now need a predicate that deletes all the terms that unify with order(X, a), that is, deletes all the orders that have a as location from that list.
For instance, this is an example of list (printed this way to make it readable):
order(tea,a)
order(tea,b)
order(coffee,b)
order(water,c)
order(lemonade,d)
order(biscuits,a)
order(water,c)
order(tea,c)
order(coffee,d)
order(water,d)
applying such needed predicate my_delete(List, [order(_, a), order(_, b)], Result) would give:
order(water,c)
order(lemonade,d)
order(water,c)
order(tea,c)
order(coffee,d)
order(water,d)
So far I've tried to remove a sublist from the main list, but what it does is just delete a single element for a and a single element for b, not all of them. This is the code for such predicate (thanks also to this reference):
remove_list([], _, []).
remove_list([X|Tail], L2, Result):-
member(X, L2),
!,
remove_list(Tail, L2, Result).
remove_list([X|Tail], L2, [X|Result]):-
remove_list(Tail, L2, Result).
and a query that I tried, but didn't work as expected, was:
remove_list(Input_list, [ordine(_, a), ordine(_, b)], Result).
Notice that I need duplicates, so using sets won't work.
You can use negation \+ to avoid further unification over recursion of your filter list:
remove_list([], _, []).
remove_list([X|Tail], ToDelete, Result):-
(\+( memberchk(X, ToDelete) ) ->
Result=[X|NResult] ;
Result=NResult
),
remove_list(Tail, ToDelete, NResult).
Using exclude/3, which takes a predicate, input list and output list:
rev_memberchk(List, Member) :-
memberchk(Member, List).
my_delete(Input_List, Orders, Result) :-
exclude(rev_memberchk(Orders), Input_List, Result).
Using memberchk/1 rather than member/2 for efficiency; you don't need the bound output, just to know if it can unify. If you also have lambda expressions, this can be turned into a one-liner by removing the need to write a predicate with re-ordered arguments:
my_delete(Input_List, Orders, Result) :-
exclude({Orders}/[X]>>memberchk(X, Orders), Input_List, Result).

Get every first element from a list of lists

I have a list of lists [[1,2,4],[5,6,7,8],[7,8,9]]
I want to get every first element of the list, i.e. [1,5,7]
Here's a solution using maplist/3 and nth0/3:
first_of_each(List, Result) :- maplist(nth0(0), List, Result).
Here's a solution using recursion:
first_of_each([], []).
first_of_each([[First|_]|Xs], [First|Ys]) :- first_of_each(Xs, Ys).
Usage example:
?- first_of_each([[1,2,4],[5,6,7,8],[7,8,9]], Y).
Y = [1, 5, 7].

remove element from list without removing all duplicates

i want remove elements from a list but if i do this:
deletelist([3,1,2,3,4], [3], [3,1,2,4])
how remove only a 3 and not get this answer:
deletelist([3,1,2,3,4], [3], [1,2,4])
Thanks!
select/3 it's an useful builtin, frequently used to generate and test, and you can use to delete an element:
?- select(3,[3,1,2,3,4],L).
L = [1, 2, 3, 4] ;
L = [3, 1, 2, 4] ;
false.
each call removes a match, then you can control the desired behaviour
edit
to delete all elements from the second list:
deletelist(L, [], L).
deletelist(With, [D|Ds], Without) :-
select(D, With, WithoutD),
deletelist(WithoutD, Ds, Without).
Note that this will fail if any of elements to be deleted will not be found in list. To avoid this, apply a 'if .. then .. else ..'
deletelist(L, [], L).
deletelist(With, [D|Ds], Without) :-
( select(D, With, WithoutD)
-> deletelist(WithoutD, Ds, Without)
; deletelist(With, Ds, Without)
).
Now deletelist/3 will not enumerate all possible deletions. It commits to the first found. To resume the initial behaviour, that give on bactracking all different deletions, a less efficient procedure is required:
deletelist(L, [], L).
deletelist(With, [D|Ds], Without) :-
select(D, With, WithoutD),
deletelist(WithoutD, Ds, Without).
deletelist(With, [D|Ds], Without) :-
\+ select(D, With, _),
deletelist(With, Ds, Without).
If you want to get just unique values in list - use function list_to_set/2:
list_to_set([3,1,2,3,4],X) gives X = [3, 1, 2, 4].
EDIT:
So gnu prolog doesn't have built-in predicate list_to_set. You have to write it yourself.
For this you must define set concept. What is set of list ? Set should have these attributes:
Set size must be less or equal to List size.
All members of Set must be members of List.
All members of List must be members of Set.
Set must not contain duplicated values.
Based on these assumptions you can write set_from_list predicate like this:
elem_unique(Elem, List) :-
delete(List, Elem, ListWithoutElem),
length(List, OrgLength),
length(ListWithoutElem, DelLength),
DelLength + 1 =:= OrgLength.
nth_elem_isUnique(N, List) :-
nth1(N, List, Elem),
elem_unique(Elem, List).
nth_elemOfList1_isMemberOfList2(N, List1, List2) :-
nth1(N, List1, Elem),
member(Elem, List2).
elements_from_nth_areUnique(N, List) :-
(length(List, Len),
N > Len) %stoping condition for recursion
;
(nth_elem_isUnique(N, List),
M is N + 1,
elements_from_nth_areUnique(M, List) %recursion part
).
listIsUnique(List) :-
elements_from_nth_areUnique(1, List).
elements_from_nth_inList1_areMembersOfList2(N, List1, List2) :-
(length(List1, Len),
N > Len) %stoping condition for recursion
;
(nth_elemOfList1_isMemberOfList2(N, List1, List2),
M is N + 1,
elements_from_nth_inList1_areMembersOfList2(M, List1, List2) %recursion part
).
list2containsList1(List1, List2) :-
elements_from_nth_inList1_areMembersOfList2(1, List1, List2).
set_from_list(Set, List) :-
length(Set, LenSet),
length(List, LenList),
LenSet =< LenList,
list2containsList1(List, Set),
list2containsList1(Set, List),
listIsUnique(Set),
!.
So after calling set_from_list(Set, [3,1,2,3,4]) you will get Set = [3,1,2,4].

Prolog programs - how to make it work?

I have these two programs and they're not working as they should. The first without_doubles_2(Xs, Ys)is supposed to show that it is true if Ys is the list of the elements appearing in Xs without duplication. The elements in Ys are in the reversed order of Xs with the first duplicate values being kept. Such as, without_doubles_2([1,2,3,4,5,6,4,4],X) prints X=[6,5,4,3,2,1] yet, it prints false.
without_doubles_2([],[]).
without_doubles_2([H|T],[H|Y]):- member(H,T),!,
delete(H,T,T1),
without_doubles_2(T1,Y).
without_doubles_2([H|T],[H|Y]):- without_doubles_2(T,Y).
reverse([],[]).
reverse([H|T],Y):- reverse(T,T1), addtoend(H,T1,Y).
addtoend(H,[],[H]).
addtoend(X,[H|T],[H|T1]):-addtoend(X,T,T1).
without_doubles_21(X,Z):- without_doubles_2(X,Y),
reverse(Y,Z).
The second one is how do I make this program use a string? It's supposed to delete the vowels from a string and print only the consonants.
deleteV([H|T],R):-member(H,[a,e,i,o,u]),deleteV(T,R),!.
deleteV([H|T],[H|R]):-deleteV(T,R),!.
deleteV([],[]).
Your call to delete always fails because you have the order of arguments wrong:
delete(+List1, #Elem, -List2)
So instead of
delete(H, T, T1)
You want
delete(T, H, T1)
Finding an error like this is simple using the trace functionality of the swi-prolog interpreter - just enter trace. to begin trace mode, enter the predicate, and see what the interpreter is doing. In this case you would have seen that the fail comes from the delete statement. The documentation related to tracing can be found here.
Also note that you can rewrite the predicate omitting the member check and thus the third clause, because delete([1,2,3],9001,[1,2,3]) evaluates to true - if the element is not in the list the result is the same as the input. So your predicate could look like this (name shortened due to lazyness):
nodubs([], []).
nodubs([H|T], [H|Y]) :- delete(T, H, T1), nodubs(T1, Y).
For your second question, you can turn a string into a list of characters (represented as ascii codes) using the string_to_list predicate.
As for the predicate deleting vovels from the string, I would implement it like this (there's probably better solutions for this problem or some built-ins you could use but my prolog is somewhat rusty):
%deleteall(+L, +Elems, -R)
%a helper predicate for deleting all items in Elems from L
deleteall(L, [], L).
deleteall(L, [H|T], R) :- delete(L, H, L1), deleteall(L1, T, R).
deleteV(S, R) :-
string_to_list(S, L), %create list L from input string
string_to_list("aeiou", A), %create a list of all vovels
deleteall(L, A, RL), %use deleteall to delete all vovels from L
string_to_list(R, RL). %turn the result back into a string
deleteV/2 could make use of library(lists):
?- subtract("carlo","aeiou",L), format('~s',[L]).
crl
L = [99, 114, 108].
while to remove duplicates we could take advantage from sort/2 and select/3:
nodup(L, N) :-
sort(L, S),
nodup(L, S, N).
nodup([], _S, []).
nodup([X|Xs], S, N) :-
( select(X, S, R) -> N = [X|Ys] ; N = Ys, R = S ),
nodup(Xs, R, Ys).
test:
?- nodup([1,2,3,4,4,4,5,2,7],L).
L = [1, 2, 3, 4, 5, 7].
edit much better, from ssBarBee
?- setof(X,member(X,[1,2,2,5,3,2]),L).
L = [1, 2, 3, 5].

Predicate to filter non constants from a list

I'm working on a predicate only_atoms/2(List+, Result-) that I'd like to filter non atoms.
For example :
only_atoms([1, 2, X, h(Y), 'aba'], Result).
should return
Result = [1, 2, 'aba'].
I do not care about the order.
Here is the piece of code I came up with :
only_atoms([], []) :- !.
only_atoms([Head | Tail], [Head | Result]) :-
atom(Head),
!,
only_atoms(Tail, Result).
only_atoms([_ | Tail], Result) :-
only_atoms(Tail, Result).
I thought this was the right kind of reasoning to handle such a problem but seem to be wrong since it yields me [](edit : it actually yields [aba], see precisions below, my bad !) no matter what. I'd appreciate some help !
A first hint: for 1 and 2, atom returns false.
By the way, I was looking for the filter predicate, in the standard library it happens to be called include, it's usually better if you use what the language already provides ;-)
?- include(atom, [1, 2, X, h(Y), 'aba'], Result).
Result = [aba].
or if you wanted just to filter out variables:
?- exclude(var, [1, 2, X, h(Y), 'aba'], Result).
Result = [1, 2, h(Y), aba].
Another by the way, one curious difference between your only_atoms and using include(atom, ...) is that yours will unify variables in the first list with atoms in the second list, whereas the include won't.
?- only_atoms([1, x, 2, Y], [x, y]).
Y = y.
?- include(atom, [1, x, 2, Y], [x, y]).
false.
Those subtleties of Prolog always astonish me (I guess that's because I didn't pay enough attention at the university xD).
You probably need to force the Head not to be an atom on the alternate clause, otherwise it is an option for atoms as well.
This returns Result = ['aba'] for me.
only_atoms([], []).
only_atoms([Head | Tail], [Head | Result]) :- atom(Head), !, only_atoms(Tail, Result).
only_atoms([Head | Tail], Result) :- \+atom(Head), !, only_atoms(Tail, Result).
Alternatively, you could try using findall/3.
atoms_list(List, Result) :- findall(Item, (member(Item, List), atom(Item)), Result).

Resources