I have one list of lists like this: [[a,b],[b,c],[c,d]] and I want to has [a,b,c].
My code is:
unMakeTuple([],_).
unMakeTuple([[A,_]|T],Ret):-
insertOnList(A,Ret,Ret1),
nl,write(Ret),
nl,write(Ret1),
unMakeTuple(T,Ret1).
insertOnList(E,[],[E]).
insertOnList(E,[H|T],[H|T1]):-
insertOnList(E,T,T1).
And return me a empty list.
Someone can help me?
Thank you.
You here want basically make a mapping where we obtain the head of every sublist.
So we can define a predicate head/2:
head([H|_],H).
and then use maplist/3:
unMakeTuple(A,B) :-
maplist(head,A,B).
then we obtain:
?- unMakeTuple([[a,b],[b,c],[c,d]],X).
X = [a, b, c].
Although writing a custom predicate can of course be beneficial as well. The advantage here is that (a) it is very declarative and easy to understand that we map a list A to a mapB by for each element unify the elements of the two lists by the head predicate; (b) we can be quite certain that maplist/3 works correctly and (c) we can expect that maplist/3 will be optimized such that processing is done fast.
#Willem Van Onsem's solution using maplist/3 above is the better way to do this, but another way to do this is to just write a predicate that adds all the heads of the inner lists to a resulting list. Some sort of recursive solution like this should work:
head([H|_], H).
get_heads([], []).
get_heads([X|Xs], List) :-
head(X, H),
List = [H|Rest],
get_heads(Xs, Rest).
Which works as follows:
?- get_heads([[a,b],[b,c],[c,d]], X).
X = [a, b, c].
Related
Given string lists [a, b, c] and [d, e] for example,
return the list [ad, bd, cd, ae, be, ce].
Please don't give me an answer, just a point the the right direction for a new learner.
This problem is tricky because you need to traverse two lists. If you write it as one function there are two recursions to keep track of.
One way to approach it is to start with a simpler problem. Say you have a list of strings l and a string s. Can you write a function that makes a new list with s appended to each of the strings in l?
There is a straightforward solution to this simpler problem using List.map. Or you can write an explicitly recursive function.
After that you have only one remaining recursion to figure out, which may be easier.
Update
Now you have your function you can easily write a function that appends a string to all the elements of the first list of strings. The layout would look like this:
let cross_concat list1 list2 =
let concat_string_to_list1 s = concat_string_to_list list1 s in
...
With this definition, you can use List.map again to get the final results (need to concatenate the resulting lists of strings into one list). Or you can use List.fold_right to build up the result as you go. Or you can write your own recursive function.
If you haven't written too many recursive functions, this would be something to think through.
If you had a definition of check_entries(X,Y)., does anyone know what would be the best way to check two lists recursively and to iterate through those lists and compare each element to see if they are different or not? I know you would use the =\= operator in there somewhere but I'm not sure what else to do...
Please help, thanks. Recursion is a weak point of mine :-(.
More specifically, what I want to know is how to check that two lists of numbers L1, L2 are all different at each index. I'd also like to know how to use =\= to check for the inequality.
Recursion isn't necessary:
all_different(L1, L2) :- forall(member(X, L1), \+ memberchk(X, L2)).
Edit: Indeed, your question is actually to ensure that if you match the lists up one-by-one, they are different. So these two would be different: [1,2,1] and [2,1,2].
I still wouldn't bother with recursion, though I guess you could:
all_different(L1, L2) :- maplist(=\=, L1, L2).
I'm learning Prolog right now and the recursive thinking is difficult to me.
I have two lists e.g. L1=[1,2,3] and L2=[3,1,2] and I should check if all elements of L1 are contained in L2
So allMembers(L1,L2). should return true and allMembers(L1,[2,3,4]). should return false, because there is no 4 in L1
I thought it would be possible to get the head of the one list and return true if it finds that element in the other list (by splitting it down until the list only contains one item), but I have no idea how to do that in Prolog.
It would be nice if you could help me getting faster on the uptake (?)
I found similar problems here, but couldn't find an answer to my specific problem :/
Edit:
I now have:
isMemberOf(X,[X|_]).
isMemberOf(X,[_|T]):- member(X,T).
which I need to use for any element in L1 to L2
Quick solution using de facto standard predicates:
all_from_first_in_second(List1, List2) :-
forall(member(Element,List1), member(Element,List2)).
Probably the simplest way to define it may be:
% contained_in(L1, L2) succeeds if all elements of L1 are contained in L2
contained_in(L1, L2) :- maplist(contains(L2), L1).
contains(L, X) :- member(X, L).
maplist/2 will apply contains(L2) to each element of L1, and will succeed if every call succeeds. contains/2 is like member/2 but puts the arguments in the order that make it work with maplist.
To solve your sub-problem: member(A,B) is true if A is a member of the list B.
Simply use list_permuted/2 like this:
?- list_permuted([1,2,3],[3,1,2]).
true.
Note that list_permuted/2 demands that both lists have the same items in the same quantity!
This solves the question with only self defined predicates:
% allMembers(L1,L2) checks if all elements of L1 are available in L2
% solve the sub problem
isMemberOf(X,[X|_]).
isMemberOf(X,[_|T]):- isMemberOf(X,T).
% iterate through 'L1' and ask if the member exists in 'L2'
allMembers([],_).
allMembers([H|T],L):- allMembers(T,L), isMemberOf(H,L).
I'm attempting to write a predicate to remove elements from an ordered list in Prolog. This is part of a homework assignment and I'm very confused about how Prolog's semantics work in general.
When I try the following function with the goal rdup([1,2], L). I get false. I've traced the goal and it looks like I'm not supposed to be building up the result list the way I am building it up with recursive calls to rdup. I'm not certain how I should be building up the result list. Here's the function:
rdup([],M).
rdup([X],[X]).
rdup([H1,H2|T], M) :- H1 \= H2, rdup(T, [M,H1,H2]).
rdup([H1,_|T], M) :- rdup(T, [M,H1]).
Can anyone tell me where my reasoning is wrong or how one is supposed to build up a list recursively in Prolog?
Firstly, it's not functions.
It's predicates, they only say, what's true, what's not, and under what conditions.
Here are your program:
rdup([],[]).
rdup([X],[X]).
rdup([H,H|T], M) :- rdup([H|T], M).
rdup([H1,H2|T], [H1|M]) :- H1 \= H2, rdup([H2|T], M).
Now a little bit of explanation.
Firstly, what says "rdup(X,Y)"? It's not saying "Take ordered list in X, and put list X without duplicates in Y", but it says "This fact will be true, if Y is an list X without duplicates, in assumption that X is an ordered list". Notice, that we don't talk about "returning values" or sth like that.
First line says, that the empty list is a list without duplicates of empty list. Quite obvious, right?
Next line is basically the same, but with one element.
Third line says, that if we have list, which consists from two same elements H and tail T, then the ordered list of this list ([H,H|T]) is the same, as it would be with only one element H. That's why we have "M" in both predicates unmodified.
I hope you will analyse the last predicate on your own, Prolog is not as hard as it looks to be. Good luck!
I'm trying to increment every element of list by its depth. For example:
foo([0, 0, [0]], [1, 1, [2]]) -> true
Also, I'd like to do that without any built-in Prolog list predicates. Any ideas on how to solve this?
For divide_half, you need to verify 2 things (which can be done independently): that the lists define some kind of split, and that parts of the split have close enough lengths.
For the second, start by trying to FIND each element, then modify that to track the depth, and finally modify THAT to build up a copy of the list w/ the elements modified (though I don't understand what it means to one-increment something by a number).
This definition divides a list without any built-in predicates:
halve(List, A, B) :- halve(List, List, A, B).
halve(B, [], [], B).
halve(B, [_], [], B).
halve([H|T], [_,_|T2], [H|A], B) :-
halve(T, T2, A, B).