GNU Prolog - Build up a list in a loop - list

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.

Related

Remove Multiple Elements in a Python List Once

(Using Python 3)
Given this list named numList: [1,1,2,2,3,3,3,4].
I want to remove exactly one instance of “1” and “3” from numList.
In other words, I want a function that will turn numList into: [1,2,2,3,3,4].
What function will let me remove an X number of elements from a Python list once per element I want to remove?
(The elements I want to remove are guaranteed to exist in the list)
For the sake of clarity, I will give more examples:
[1,2,3,3,4]
Remove 2 and 3
[1,3,4]
[3,3,3]
Remove 3
[3,3]
[1,1,2,2,3,4,4,4,4]
Remove 2, 3 and 4
[1,1,2,4,4,4]
I’ve tried doing this:
numList=[1,2,2,3,3,4,4,4]
remList = [2,3,4]
for x in remList:
numList.remove(x)
This turns numList to [1,2,3,4,4] which is what I want. However, this has a complexity of:
O((len(numList))^(len(remList)))
This is a problem because remList and numList can have a length of 10^5. The program will take a long time to run. Is there a built-in function that does what I want faster?
Also, I would prefer the optimum function which can do this job in terms of space and time because the program needs to run in less than a second and the size of the list is large.
Your approach:
for x in rem_list:
num_list.remove(x)
is intuitative and unless the lists are going to be very large I might do that because it is easy to read.
One alternative would be:
result = []
for x in num_list:
if x in rem_list:
rem_list.remove(x)
else:
result.append(x)
This would be O(len(rem_list) ^ len(num_list)) and faster than the first solution if len(rem_list) < len(num_list).
If rem_list was guaranteed to not contain any duplicates (as per your examples) you could use a set instead and the complexity would be O(len(num_list)).

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

Reading and storing all words of file in prolog

I am a newbie to prolog, till now I am able to read all words of file, displayed them one by one, now I want to store them in a list(one by one, as I soon as I am displaying them). All logic for append given everywhere, append content of two lists in an empty list. For example
append(new_word,word_list,word_List), intially my word_list is empty, so everything fine, but afterwards it says no, and stop at that point.
Need help to be able to store element in list one by one.
You can use difference lists :
file_to_list(W, L) :-
read_word(Word),
append_dl(W, [Word|U]-U, Ws),
!, file_to_list(Ws, L).
file_to_list_1(Ws, Ws).
append_dl(X-Y, Y-Z, X-Z).
You call file_to_list(U-U, L-[]) to get the list of words. There is no slowdown but takes more inferences than CapelliC's code (one per word).

Prolog permutations with repetition

I'm having a hard time wrapping my head around the concept of logic programming. I'm trying to get all permutations with repetition into a give list.
I can put what I have, but I don't know what I'm doing!
perms_R(List,[]).
perms_R([X|Xt],[Y|Yt],Out) :- perms_R([Y|Xt],Yt),perms_R(Xt,[Y|Yt])
.
The idea was to go through each element in the second list and put it in my first list. I'm trying to figure this out, but I'm stuck.
I need to call perms_R([a,b,c,d],[1,2,3,4]). and get:
1,1,1,1
1,1,1,2
1,1,1,3
1,1,1,4
1,1,2,1
etc....
I understand the first list seems useless and I could just do it with a list length, but I actually need it for the remainder of my code, so I'm trying to model this after what I need. Once I get past this part, I will be putting extra logic in that will limit the letters that can be replaced in the first list, but don't worry about that part!
What you are looking for is not a permutation. You want to create a list of a given size using items from a given set.
You may do it with this snippet:
perms_R([], _).
perms_R([Item|NList], List):-
member(Item, List),
perms_R(NList, List).
You would need to pass a semi instantiated list and the source items:
perms_R([A,B,C,D],[1,2,3,4]).