How to make a list of lists in prolog - list

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.

Related

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.

Prolog list of "lists" with only one element

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).

Prolog Combining Two Lists

I am new to prolog and would appreciate any help on the following question:
I need to write a program that accepts two lists and appends the second to first and displays this new list and its length. I know that prolog might have some built in functions to make this all easier...but I do not want to use those.
eg: newlist([a,b,c],[d,e,f],L3,Le). would return L3=[a,b,c,d,e,f] and Le=6
Here is what I have so far:
newlist([],List,List,0)
newlist([Element|List1],List2,[Element|List3],L) :- newlist(List1,List2,List3, LT), L is LT + 1.
This does the appending correctly but I can only get the length of the first list instead of the combined list. Is there a way for me to add the second list's length to the first to get the combined list length?
Thanks, and sorry if this question is rather easy...I am new.
Is there a way for me to add the second list's length to the first to get the combined list length?
You should replace:
newlist([],List,List,0).
with:
newlist([],List,List,X):-length(List,X).

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.