Prolog checking if 2 lists have same number of elements - prolog

I am trying to figure out how to do this little thing. I have 2 lists for example [1,1,2,3,4] and [2,1,4,3,1], I need to confirm if all elements from list 1 are included in list 2, soo if i give the above lists as input this should be true, but if its like this [1,1,2,3,4] and [2,1,4,3,1,1] (three 1's) it should give false, this has to be done without using sort function.

I assume you know how to write a list as head and tail ([H|L]).
So you could use the predicate member/2 to ask for every element from the first list to be in the second list as well, but this would not solve the issue of duplicates. Using the predicate length/2 will not help in this case. So you need something that retracts one matching element from a list. You can either write your own find-and-remove-predicate or use the predicate append/3 to do so. append/3 is thought to append 2 lists to form a third one, but it can also be used to divide one list into two. If you state that your element as the head element of the second divided list you basically get a 'remove element' functionality. Once you've got the 2 divided lists, combine them into a new list and call the predicate again, but this time without the head element from list one and with the reappended-list. So in each step you remove one element from each list until you finally hit two empty lists (permut([],[]).). If something other than this two cases should appear, then the two lists are not permuations of each other and the predicate fails.
Since there is no use in backtracking other positions I inserted a cut (!) after successfully finding an element in the second list. The predicate works without the cut as well.
permut([],[]).
permut([H|T], Compare):-
append(C1, [H|C2], Compare),
!,
append(C1, C2, Cnext),
permut(T, Cnext).
gives the output
?- permut([1,2,3,4,5],[5,4,3,2,1]).
true.
?- permut([1,2,3,4,5],[5,4,3,2,1,1]).
false.
?- permut([1,2,3,4,5,6],[5,4,3,2,1]).
false.

Related

update element in the list of list in prolog

I have a list of list and a list. I want to update the list of list by using the element from the second list.
For example:
I have a list of list
[[banan,NA],[apple,NA]] and a list [sweet,notsweet],
want to update the list of the list, so I will have a list of list
[[banana,sweet],[apple,notsweet]]
I have tried the code below, but I think I cannot figured out the base case correctly.
update([[]],[],[]).
update([[T|_]|HH],[FB|H2],[NState|_]) :-
NState=[T|FB],
update(HH,H2,NState).
Any help, will be much appreciate
thanks
You're almost there. First let's observe that the first list is empty if there are no more [fruit,*] pairs left, hence the first argument of your base case should be []. At that point the other lists have to be empty too, since they are of the same length.
In general, the first list will have a two-element list as its head, the first of which is being your object of interest and the second of which you don't care for, that is, something like [X,_]. The tail of that list will contain further X's, so let's maybe call it Xs. Then the first argument looks like [[X,_]|Xs]. The second argument is a flat list, so you can write [Y|Ys] (read as: the list starts with a Y that is followed by further Y's). The last argument is a two-element list [X,Y] that is followed by other such pairs (XY's), hence: [[X,Y]|XYs]. The relation has to hold for the tails as well, that can be described by a recursive goal. You can express the above in Prolog like so:
update([],[],[]).
update([[X,_]|Xs],[Y|Ys],[[X,Y]|XYs]) :-
update(Xs,Ys,XYs).
With these alterations to your predicate the example query from your comment yields the desired answer:
?- update([[banana,*],[apple,*]],[sweet,notsweet],C).
C = [[banana, sweet], [apple, notsweet]].

Get all Prefixes of a List, including [] in Prolog

I am new to Prolog and wanted to start learning the functionality of [H|T] by trying to write the prefix function on my own. The prefix function returns all possible prefixes to a list, such as for L = [a,b,c] would be [], [a], [a,b] and [a,b,c]. I am confused about how my own Prolog function would be able to return so many different solutions. I have it splitting the head and tail of the list, and appending the head to my new list that is going to be the different prefixes returned. This is what I have so far, but I think I am oversimplifying, and don't know how else to recursively call it to get all the different possibilities.
myPrefix([],[]).
myPrefix([H|T],List) :- myPrefix(T, [H|List]).
I looked at a lot of the other answers, but those just deal with getting the first element off of a list, not listing all possible prefixes. Any advice of how to go from here or more explanation as to how this head and tail of list manipulation functionality works is appreciated.
Assuming the first argument if instantiated is indeed is a proper list, then this should do it:
myPrefix(_, []).
myPrefix([H|T], [H|NT]):-
myPrefix(T, NT).
First clause ignores the first argument and unifies the second with the empty list.
Second clause takes the head of first argument list and puts it as the head of the second argument, and calls itself recursively.
So in effect, the second clause takes one by one as many items as desired and the first clause drops the tail.

Write Prolog ordered predicate

I am trying to write a predicate that succeeds if and only if the numbers in the list are in non-decreasing order. I am having a hard time trying to figure this out. I know that if each element is less than or equal to the previous one then it should return false but I am lost on how to do it.
ordered(L) :-
Recursion should usually be your first thought for approaching any problem in Prolog. This means:
Defining a base case, where you can easily determine that the predicate is true or false
In other cases, splitting the problem into parts - one part you can resolve immediately, another you can resolve recursively. These parts of the problem generally correspond to portions of the list.
In the simplest cases, the recursive logic is simply to apply some test to the first element of the list; if it passes, recursively apply the predicate to the remainder of the list.
In your case I think it is a bit more complex, as there is no meaningful way you can test an individual element for orderedness (and maybe that gives you a hint what the base case is ...).
ordered(L) :- ordered2(L).
% empty list is ordered
ordered2([]) :- true.
% list with one element is ordered
ordered2([_]) :- true.
% list is ordered if two first elements are ordered
% and the rest of list is ordered
ordered2([A,B|T]) :- A=<B, ordered2([B|T]).

how to compare a list's elements 3 at a time in prolog

Given a list (A) I want to be able to create a new list (B) that contains only the elements of A that are the smallest or the biggest compared to their next and previous element. My problem is that I don't know how to do the comparisons of each element with its previous one.
(This question may be silly but I'm new to prolog and any help would be appreciated.)
You could start with something like that:
compareElem([]).
compareElem([H,H1,H2|B]):-compareElem(B),
compare(?Order, H1,H2),
compare(?Order, H1, H).
where ?Order is the order of comparison (like '<' or '>'). See compare/3.
Some queries:
?- compareElem([1,2,3,4,5,6]).
true.
?- compareElem([1,2,3,4,5,3]).
false.
of course to apply this example you must ensure that the list has 3n elements, this is just a basic example. Together with this comparison you can generate the other list

How to make an operation to every element of a list

I find myself narrowing a (very simple) problem more and more.
Let's say I have this operation: listsplit([H1,H2,H3|T], H1,H2,H3, T).
Which gives me the first three elements of a list. I want a program, cells, to travel an input list and make (at least that ONE operation!!) to every element of the list.
So I have something like:
cells(Input, Result):-
cellsBody(Input, [], Result).
cellsBody([],Result,Result).
cellsBody([Head|Input], Acc, [Headd|Result]):-
listsplit(Input,H1,H2,H3,_),
cellsBody(Input, [OutputBody|Acc], Result).
I have that code because I have used many I've seen as examples that go like that to travel a list. They separate head from body and go on. I fail to see how this is done in prolog. I tried changing variable names, so that they would match (as I would do in other languages), and I've tried to make the problem as simple as possible.
So, how do I travel a list AND make operations to every element (that I choose to, starting with the first one, the head).
Edit: Examples of what I want to archieve:
I get an input list like oxo, oxxxo, oxoxo, so on. I then apply a rule to the first three elements, then the next three, and so on, and while I do that I add the result of the rule to another list that I return (which is why I am trying to use the accumulator).
You've almost got it. Keeping your predicate cells/2 as is, think about the special cases first: The lists [], [_], [_,_] haven't got three elements, so whatever operation you have in mind for those three elements, there's nothing to do in these cases. Otherwise you have a recursive rule to do what you intend to.
Looking at listsplit/5: you can do that directly in the head of the recursive rule, no need for an extra predicate. Then you have one or more goals for your intended operation. For the sake of an example let's say packaging the 3 head elements as a triplet. And of course the relation must hold for the tail T of the list too. Then your code might look something like that:
cellsBody([],Result,Result).
cellsBody([_],Result,Result).
cellsBody([_,_],Result,Result).
cellsBody([H1,H2,H3|T], Acc, Result):- % the first 3 elements
Triplet=(H1,H2,H3), % at least ONE operation with them
cellsBody(T, [Triplet|Acc], Result).
Example queries:
?- cells([],Result).
Result = []
?- cells([1],Result).
Result = []
?- cells([1,2],Result).
Result = []
?- cells([1,2,3],Result).
Result = [(1,2,3)]
?- cells([1,2,3,4,5,6,7],Result).
Result = [(4,5,6),(1,2,3)]
Of course, if the intended operation is as simple as in the above example, you don't need an extra goal for it: You can do that directly in the recursive goal:
cellsBody([H1,H2,H3|T], Acc, Result):-
cellsBody(T, [(H1,H2,H3)|Acc], Result).

Resources