PROLOG sum of integers inside a list - list

I have a list of functions
List = [segmentTime(red,a,c,2),segmentTime(green,c,e,3),segmentTime(green,e,h,4),segmentTime(blue,h,i,5)]
How do I find the sum of the integer part of the function of all elements in the list?
i.e
sum = 2+3+4+5
A snippet code of a predicate would be extremely useful.
Thanks in advance :)

You will be surprised how simple the answer is.
sumList([],0).
sumList([segmentTime(_,_,_,X)|T],Z):- sumList(T,Z1),Z is Z1+X.
OUTPUT
?-sumList([segmentTime(red,a,c,2),segmentTime(green,c,e,3),segmentTime(green,e,h,4),segmentTime(blue,h,i,5)],M).
M=14
Hope this helped you.

Another way is to use library(lambda).
:- use_module(library(lambda)).
getList(L):-
L = [segmentTime(red,a,c,2),segmentTime(green,c,e,3),segmentTime(green,e,h,4),segmentTime(blue,h,i,5)].
sumOfList(L, S) :-
foldl(\X^Y^Z^(X = segmentTime(_,_,_,V), Z is Y + V), L, 0, S).
Ouput :
?- getList(L), sumOfList(L, S).
L = [segmentTime(red, a, c, 2), segmentTime(green, c, e, 3), segmentTime(green, e, h, 4), segmentTime(blue, h, i, 5)],
S = 14.

Related

How to simplify algebra equations represented as list of list

With Prolog I want to simplify algebra expression represented as as list of list:
algebra equation
f = 3x+2
list of list
[[3,1],[2,0]]
3 and 2 are coefficients
1 and 0 are exponents
That should be obvious.
I am looking for some tips or suggestions on how to code the simplifications for this example:
f = 3x+2x+1+2
[[3,1],[2,1],[1,0],[2,0]]
simplified:
f = 5x+3
[[5,1],[3,0]]
I have tried some built in functions but did not get the proper idea about how to use them.
One liner, similar to what's proposed by joel76:
simplify(I,O) :-
bagof([S,E],L^(bagof(C,member([C,E],I),L),sum_list(L,S)),O).
The inner bagof collects C (coefficients) given E (exponents), the resulting list L is summed into S, and paired with E becomes [S,E], an element (monomial) of O.
If you omit the universal quantification specifier (that is L^) you get single monomials on backtracking.
You can solve your problem in this way:
simplify(_,_,S,S,[]):- !.
simplify(L,I,Sum,NTot,[[I,S]|T]):-
Sum =< NTot,
findall(X,member([X,I],L),LO),
length(LO,N),
S1 is Sum + N,
sum_list(LO,S),
I1 is I+1,
simplify(L,I1,S1,NTot,T).
write_function([]).
write_function([[D,V]|T]):-
write(' + '),write(V),write('x^'),write(D),
write_function(T).
test:-
L = [[3,1],[2,1],[1,0],[2,0]],
length(L,N),
simplify(L,0,0,N,LO),
LO = [[D,V]|T],
write('f='),write(V),write('x^'),write(D),
write_function(T).
The main predicate is simplify/5 which uses findall/3 to find all the coefficients with the same degree and then sums them using sum_list/2. Then you can write the result in a fancy way using write_function/1.
In SWI-Prolog You can use aggregate :
pred(>, [_,X], [_,Y]) :- X > Y.
pred(<, [_,X], [_,Y]) :- X < Y.
pred(=, [_,X], [_,X]).
simplify(In, Out) :-
aggregate(set([S,X]), aggregate(sum(P), member([P,X], In), S), Temp),
predsort(pred, Temp, Out).
For example :
?- simplify([[3,1],[2,1],[1,0],[2,0]], Out).
Out = [[5, 1], [3, 0]] ;
false.

Give as a solution every different number in a list of lists

I need to do a predicate, select(ListOfLists, X) that returns as a solution every different number in a list of lists, starting with the numbers that are alone in a list, for example:
select([[1,2,3],[1,2],[4],[3]],X).
Would return:
X = 4 ;
X = 3 ;
X = 2 ;
X = 1
Order doesn't matter as long as the numbers that are alone in the list are shown first.
To do this, first I coded 2 other predicates, which are:
%OrderedList is Lists ordered by size.
orderListsBySize(Lists, OrderedLists).
Example: orderListsBySize([[1,2],[6],[3,4,5]], L). ->L = [[6], [1,2], [3,4,5]]
And
%ListsWithoutX is Lists without the X elements
removeFromLists(X, Lists, ListsWithoutX).
Example: removeFromLists(1,[[1,2],[3],[4,1,5]],L). -> L = [[2],[3],[4,5]]
Both predicates work.
Then, to do the select(ListOfLists, X) predicate, I tried the following:
select([[X|[]]|_], X). select(L1,S) :-
orderListsBySize(L1, [[X|XS]|LS]),
length(XS, A),
A == 0,
select([[X|[]]|M], S),
removeFromLists(X, [XS|LS], M).
select([[X|_]|_], X).
But it doesn't work.
It's not a hard exercise to do in other languages, the problem is that it's still hard for me to understand how prolog works. I appreaciate any help, thanks!
You could start with:
select2(ListOfLists,Element):-
length(List,_Len),
member(List,ListOfLists),
member(Element,List).
Which will return all the answers, but then get stuck in a loop looking for ever bigger lists.
This can be averted using the :-use_module(library(clpfd)). and defining a fd_length/2 which wont keep looking for bigger lists then exist in the list of lists.
fd_length(L, N) :-
N #>= 0,
fd_length(L, N, 0).
fd_length([], N, N0) :-
N #= N0.
fd_length([_|L], N, N0) :-
N1 is N0+1,
N #>= N1,
fd_length(L, N, N1).
select(ListOfLists,Element):-
maplist(length,ListOfLists,Lengths),
sort(Lengths,SortedLength),
last(SortedLength,Biggest),
Biggest #>= Len,
fd_length(List,Len),
member(List,ListOfLists),
member(Element,List).
Example Query:
?-select([[1,2,3],[1,2],[4],[3]],X).
X = 4
X = 3
X = 1
X = 2
X = 1
X = 2
X = 3
false
If you want unique solutions, you could enclose in a setof/3 and then call member/2 again.

Prolog - Give out every nth element of a list

I'm working on a Prolog program which should load every nth element of a list into another list. For example:
?- pred([a,b,c,d,e,f,g,h,i,j],3,R) =>
R = [c,f,i]
Where pred is the predicate I'm attempting to implement.
But I honestly don't know how to do it. I know that I need a counter, which represents the current position of my Head, so it's gonna be a /4 predicate summarized into a /3 one later one, like
nth(list,number,result) :- nth(list,number,result,counter) or similiar.
Though, I don't know how to give the head a position number that can reset itself. Because once it hits n (let's say n=3, which is the c in the list), it has to turn back to 1 logically and count again up to 3, give out the element, and so on.
How can I work around these specific problems in my implementation?
An example of how this could be implemented:
nth_pos(L, N, R):-
nth_pos(L, 1, N, [], R).
nth_pos([], I, N, Acc, Acc).
nth_pos([H|T], I, N, Acc, R):-
I =:= N,
append(Acc, [H], Acc2),
I2 is 1,
nth_pos(T, I2, N, Acc2, R).
nth_pos([H|T], I, N, Acc, R):-
I < N,
I2 is I + 1,
nth_pos(T, I2, N, Acc, R).
Test-run:
?- nth_pos([a,b,c,d,e,f,g,h,i,j],3,R).
R = [c, f, i] .
?- nth_pos([a,b,c,d,e,f,g,h,i,j],1,R).
R = [a, b, c, d, e, f, g, h, i|...]
But I honestly don't know how to do it. I know that I need a counter,
which represents the current position of my Head, so it's gonna be a
/4 predicate summarized into a /3 one later one, like
nth(list,number,result) :- nth(list,number,result,counter) or
similiar.
Yes you are on the right track, note that an accumulator is used as well to build up the list so we get pred/5. Hope it helps. Note that this is not the only way to solve it.

list of ordered pairs in prolog

I am trying to implement a prolog method that will take a number and return a list of all possible ordered pairs where both X and Y are less than the number given. For example
genXY(2,R).
Should return
R=[0,0];
R=[0,1];
R=[1,0];
R=[1,1].
I am having trouble understanding how to implement this. I have written the code
genN(N,R) :-
N1 is N-1,
between(0,N1,R).
Which will give the following output when executed
genN(3,R).
R=0;
R=1;
R=2;
And I believe that I should use forall to implement genXY but I don't understand how I would go about doing this
Just use between/3 twice:
?- N = 2, R = X-Y, succ(N0, N), between(0, N0, X), between(0, N0, Y).
R = 0-0;
R = 0-1;
R = 1-0;
R = 1-1;

Generate a sublist in prolog given first element and length

Given the start position and length, I want to generate a sublist of that length. I did it below using an accumulator:
get_sublist(_,_,0,Sublist,Sublist):-!.
get_sublist(List,Position,Length,Sublist,Acc):-
nth1(Position,List,Element),
append([Element],Acc,Acc1),
Position1 is Position + 1,
Length1 is Length - 1,
get_sublist(List,Position1,Length1,Sublist,Acc1).
Is there a shorter/faster way to do this either by using more built in predicates or using an alternative method? Thanks.
I would go for:
sublist(List, Offset, Length, Sublist):-
length(Prefix, Offset),
append(Prefix, Rest, List),
length(Sublist, Length),
append(Sublist, _, Rest).
Example of use:
?- sublist([a,b,c,d,e,f,g,h,i,j,k], 3, 6, X).
X = [d, e, f, g, h, i].
Notice I'm using count-by-0 for the Offset argument here.
if I run your code, I get
?- get_sublist([a,b,c,d,e,f,g,h,i,j,k], 3, 6, X, []).
X = [h, g, f, e, d, c].
then, a reverse/2 is missing ? In case, I would propose the good old findall/3:
get_sublist(List,Position,Length,Sublist):-
Stop is Position+Length-1,
findall(X, (between(Position,Stop,P),nth1(P,List,X)), Sublist).
edit bagof/3, by means of declaration of scoped variables (just P here), will avoid problems introduced by findall:
get_sublist(List,Position,Length,Sublist):-
Stop is Position+Length-1,
bagof(X, P^(between(Position,Stop,P),nth1(P,List,X)), Sublist).
?- get_sublist([A,B,C,D,E,F,G,H,I,J,K], 3, 6, X).
X = [C, D, E, F, G, H].