Prolog list of "lists" with only one element - list

I have a recursive predicate call, and the output of that call is appended to a list with append/3. So the output is going to be a list of lists. My problem is, that sometimes the append/3 gets called something like this:
append([[2]], [1,2,3,4], L).
And the output of this is [[2], 1,2,3,4]. I would like to check before the append/3 that the first element is a list of lists with only one element, but so far I was unable to create a pattern that matches it.
So my question is: how can I check if something is a list of a list?

The rule that matches a Prolog atom is, well, atom/1:
is_list_of_list_with_one_element([[X]]) :- atom(X).

Related

How to find and output lists including the max number(of all lists)? Prolog

I need to find the max number in a list of lists in Prolog, and then 'print' (output in a new list) the lists that include this max number.
For example: lists_with_max([[1,2,3],[2,3,44],[44,5,6]],Lists).
Should output: Lists = [[2,3,44],[44,5,6]]
Code below is the closest I could think of that works and has no errors, but it's obviously wrong because everytime it finds a new max and then outputs wrong lists.
'max' finds the max number in a list.
'flat' "flattens" a list.
'maxl' finds the max number in a list of lists.
max([X], X).
max([H|T], H):-
max(T, MaxT),
H > MaxT.
max([H|T], MaxT):-
max(T, MaxT),
H =< MaxT.
flat([], []).
flat([H|T], [H|LT]):-
atomic(H),
flat(T, LT).
flat([H|T], L):-
flat(T, LT),
not(atomic(H)),
flat(H, LH),
append(LH, LT, L).
maxl(List,Max):-
flat(List,Newlist),
max(Newlist,Max).
lists_with_max([],[]).
lists_with_max([H|T],[H|L]):-
maxl([H|T],Max),
member(Max,H),
lists_with_max(T,L).
lists_with_max([_H|T],L):-
lists_with_max(T,L).
Here's another approach using builting predicates max_list/2, maplist/3, include/3 and memberchk/2:
lists_with_max(LL, LL1):-
maplist(max_list, LL, ML),
max_list(ML, M),
include(memberchk(M), LL, LL1).
I think you went off the rails a bit with maxl/2 and flat/2, you don't really need them to solve your problem. This isn't, IMO, a great example of something you can do by throwing a bunch of recursion at the problem, you really are going to have to look through all the lists, find the max element, and then look through them all again. Your max/2 is fine though.
So, let's find the max element:
setof(Max, L^(member(L, Lists), max(L, Max)), Maxes),
This is a meta-predicate, it's just going to look through all the lists for the max of that list, and make a new list of the maxes of each list. I think good Prolog should have a fluent quality; if you reworded your query into something more like logic, you would have something like "The Maxes of the Lists is a list containing the max of each List" and eventually you would arrive at something like what you see there. The L^ is saying, we do not want to group this by the list L it generates (there will come other times in your life as a Prolog programmer where you will want this grouping). Probably using findall/3 would have the same effect, but I like setof/3 more.
Next, let's find the max-of-maxes:
max(Maxes, Max),
Pretty straight-forward. Now let's find all the lists that have the maximum in them:
setof(L, (member(L, Lists), member(Max, L)), Result).
This is another one like the first, where we're saying "find me all the lists L such that L is in Lists and Max is in L". Putting it all together we have:
lists_with_max(Lists, Result) :-
setof(Max, L^(member(L, Lists), max(L, Max)), Maxes),
max(Maxes, Max),
setof(L, (member(L, Lists), member(Max, L)), Result).
Another way of doing max/2, which I include here mostly to show you another way to think about Prolog because it is way, way less efficient and sort of terrible is this:
max(List, Max) :-
member(Max, List),
\+ (member(Y, List), Y > Max).
This isn't great style, but it does demonstrate that you can sometimes tell Prolog what you want rather than how to find it. This says the Max of List is the member of Max of List such that there is no member Y of List which is greater than it. Food for thought, I hope.

Check if all tuples in a list are different (ECLiPSe)

I have a list with tuples like this:
[(1,2),(5,3),(6,7)]
I'm trying to write a predicate that checks if all tuples in a list are different. The following line works for a list with integers but not for a list with tuples:
all_diff(L) :- \+ (select(X,L,R),member(X,R)).
member/2 works fine to check if a tuple is in a list but it is the select/3 that has problems with tuples. It gives a type error.
How do I check if all tuples are different?
#false pointed out that select/3 does not work in ECLiPSe because in that system it is a file-related operation.
I want to keep using the same system so I wrote a select for tuples:
%Given a list of tuples this checks if they are all different
allDiffTuples(L) :- \+ (selectTuple(X,L,R),member(X,R)).
selectTuple((A,B),[(A,B)|T],T).
selectTuple((A,B),[H|T],[H|R]) :-
selectTuple((A,B),T,R).

Creating a table of square roots of a given list in Prolog

I am a newbie to Prolog and trying to develop a simple code to get the following output.
?-sqrt_table(7, 4, Result).
Result = [[4, 2.0],[7, 2.64575],[6, 2.44949],[5, 2.23607]] ;
false.
However the output I'm getting is,
?- sqrt_table(4,7,X).
X = [[5, 2.23606797749979], [[6, 2.449489742783178], [[7, 2.6457513110645907], []]]].
I think the issue is in the nested list created by get_sqrt/2. If I can get it flattened down to tuples I think it might work. Your ideas and help are much appreciated.
%main predicate
sqrt_table(N,M,Result):-
full_list(N,M,Out),
get_sqrt(Out,[Rest,Result]).
%Creates a range of outputs within the given upper and lower limits
range(Low,Low,High).
range(Out,Low,High):-
NewLow is Low+1,NewLow=<High,
range(Out,NewLow,High).
%Creates a list of the outputs created by range/3
full_list(Low,High,Out):-
findall(X,range(X,Low,High),Out).
%Calculates the square root of each item in the list and gives a list consisted
%of sublists such that [Input,Squareroot of the input]
get_sqrt([],[]).
get_sqrt([H|T],[[H,Sqrt],SqrtRest]):-
SqrtOf_H is sqrt(H),
get_sqrt(T,SqrtRest),
Sqrt = SqrtOf_H.
Thanks in advance.
In the head of the second clause ofget_sqrt/2, simply write [[H,Sqrt]|SqrtRest], i.e., use (|)/2 instead of (,)/2.
In fact, it would be even better to use the more readable and more idiomatic [H-Sqrt|HSqrts], i.e., use (-)/2 do denote pairs.
And in second fact, a better way altogether is to simply state the relation for one element at a time, using for example:
integer_isqrt(I, I-Sq) :- Sq is sqrt(I).
and then to use the meta-predicate maplist/3 to relate lists of such elements to one another:
?- maplist(integer_isqrt, [0,1,2,3,4], Ls).
Ls = [0-0.0, 1-1.0, 2-1.4142135623730951, 3-1.7320508075688772, 4-2.0].
P.S.: Using flatten/2 always indicates a problem with your data structures, you should avoid flatten/2 entirely. If you need to remove one level of nesting, use append/2. But in this case, neither is needed.

How to make a list of lists in prolog

I use SWI-Prolog and I want to make a list of several other lists.
For example, I want to put the following three lists
[a,b,c]
[1,2]
[d]
into a larger one that looks like [[a,b,c],[1,2],[d]] .
divideList([]):-!.
divideList([Head|Tail]):-
list_to_set(Head,H),%H is a List
divideList(Tail).
I want to put all H in one list.
How can I do this?
The problem is that you are not using H in your predicate to put the list you want
This should do it
divideList([], []):-!.
divideList([Head|Tail], [H|HTail]):-
list_to_set(Head,H),%H is a List
divideList(Tail, HTail).
The second argument will have your list of lists.

GNU Prolog - Build up a list in a loop

I need to build a new list with a "loop". Basically i can't use recursion explicitly, so i am using append to go through lists of list.
I can get the element. Problem is i need to check this element and if something is true it returns another element i need to put back into the list. It does check correctly and it changes correctly.
Problem i am having is how do i create a completely new list.
So, if i had
[[1,1,1],[2,6,2],[3,3,3]]
I go through each element. say i get to the 6 and it changes. So i need to create a new list like so,
[[1,1,1],[2,10,2],[3,3,3]].
Right now my main problem is just creating each row. If i can create each row, i will be able to create a list of lists.
So to break this down a little more, lets just worry about [1,1,1].
I go through each element while appending the new element to a newlist. the new list is now [1,1,1]
I have this:
set(Row,Col,Bin,TheEntry,Bout) :-
append(ListLeft, [R|_], Bin),
append(ListLeft2, [C|_], R),
length(ListLeft, LenR),
length(ListLeft2,LenC),
CurrRow is LenR + 1,
CurrCol is LenC + 1,
getChar(C, Row, Col, CurrRow, CurrCol,TheEntry, NewC),
appendhere?.
I need to create a new list there with the character returned from NewC. Not sure how to do this.
Any clues?
Thanks.
To give you an idea about how to use append/3 to extract an item from a list of lists, consider the following predicate called replace/2:
replace(In, Out) :-
append(LL, [L|RL], In),
append(LE, [E|RE], L),
replaceElement(E, NewE), !,
append(LE, [NewE|RE], NewL),
append(LL, [NewL|RL], Out).
replace(In, In).
This non-recursive predicate takes, as Input, a list of lists, and backtracks to find an element E within an inner list L that can be replaced via replaceElement/2; if so, it is replaced by constructing the inner list first (NewL), then uses this new list in the construction of the new outer list (Out), as the result.
Note that this simply serves to demonstrate how to use append/3 to break apart a list of lists to retrieve individual elements as you need via backtracking, and not recursion, as requested. Once an element E is found to be replaceable by NewE via replaceElement/3, it is used in the construction of the list again using append/3 as shown.
Also note that this suggestion (which is intended to help you, not be your final answer) also happens to replace only a single element within an inner list, if any at all. If you want to do multiple replacements of the input list in a single call to replace/2 or similar using this technique, then you will almost certainly need a recursive definition, or the ability to use the global database via assert. I'm happy to be corrected if someone else can provide a definition as a counterexample.
With this example predicate replace/2, together with, say, the following fact:
replaceElement(6, 10).
Executing the following gives us your required behaviour:
1 ?- replace([[1,1,1],[2,6,2],[3,3,3]], Out).
Out = [[1, 1, 1], [2, 10, 2], [3, 3, 3]] ;
false.
If you cannot use cut (!), it is fine to omit it, but note that the second clause replace(In, In) will cause all calls to replace/2 to backtrack at least once to give you the input list back. If this behaviour is undesirable, omitting this second clause will cause replace/2 to fail outright if there is no replacement to be made.
If you cannot use recursion and have to do it with backtracking you should do something like this:
Assume Bin is a list of lists (each item is a full row)
~ Split input Bin in three parts (a list of 'left' rows, a Row, and a list of remaining rows). This can be done using append/3 with something like append(Left, [Item|Rest], Rows)
~ Now obtain the length of the 'left' rows
~ Test the length using 'is' operator to check wether the left list has Row - 1 items
~ Do the same but now with the Item, i.e. split it in three parts (LeftColums, ColumItem and Rest)
~ Test now the length against the required Column
~ Now you have the Item to change so all you need to do is rebuild a list using two appends (one to rebuild the chosen row and another to rebuild the output list).
So from your code you wouldn't use unnamed variables (_). Instead of that you have to use a named variable to be able to rebuild the new list with the item changed.