for example, if N is 2, it will create list following and set his value
L = [S1,S2],
L :: 1..10.
if N is 3 , it will crate list following and set his value
L = [S1,S2,S3],
L :: 1..10.
Thanks very much :)
To make a list of length N, use length/2 with a variable as the first argument:
1 ?- N=4, length(List,N).
N = 4,
List = [_G487, _G490, _G493, _G496].
Are you looking for smth-like
list( N, List ) :-
numlist( 1, N, List ).
which make
?- list(5, L).
L = [1, 2, 3, 4, 5].
Or what?
UPD:
kal( N, Lists ) :- govna( N, [[]], [_ | Lists] ), !.
govna( 0, Ans, Ans ).
govna( N, List, Com ) :-
numlist( 1, 10, X ),
append( List, [X], ListNew ),
NewN is N - 1,
govna( NewN, ListNew, Com ).
Is this it?
?- kal(2, X).
X = [[1, 2, 3, 4, 5, 6, 7, 8|...], [1, 2, 3, 4, 5, 6, 7|...]].
Related
Given a list L of natural numbers I need to create a list containing the sums of the elements of all the subsets of L. For example if L=[1,3,6] I want to obtain the list [0,1,3,4,6,7,9,10].
I tried to use this code
subsetSums(List,Sums) :- findall(Sum,(subset(Sub,List),sum_list(Sub,Sum)),Sums).
but with the following query I get [0] as the only result instead of [0,1,2,3]
?- subsetSums([1,2],Sums).
Where am I wrong?
EDIT: I'm working on SWI Prolog and subset/2 should be a native predicate.
As suggested in the comment, you have to write your own subset/2 predicate and then use findall/3 on this predicate, like this:
subset([], []).
subset([E|T], [E|T1]):-
subset(T, T1).
subset([_|T], L):-
subset(T, L).
subsetSums(List,Sums) :-
findall(S,(subset(List,Sub),sumlist(Sub,S)),Sums).
?- subsetSums([1,2],L).
L = [3, 1, 2, 0]
?- subsetSums([1,2,3],L).
L = [6, 3, 4, 1, 5, 2, 3, 0]
where the output of subset/2 is:
subset([1,2,3],L).
L = [1, 2, 3]
L = [1, 2]
L = [1, 3]
L = [1]
L = [2, 3]
L = [2]
L = [3]
L = []
I don't understand why my code doesn't work.
An inverse permutation is a permutation in which each number and the number of the place which it occupies are exchanged. For example [3,8,5,10,9,4,6,1,7,2] -> [8,10,1,6,3,7,9,2,5,4]
inv_perm3(X,[F],length(X)):-
length([F]) == 1,
!,
nth0(F,X,length(X)).
inv_perm3(X,[F|M],N):-
nth0(F,X,N), %nth0(?Index, List, Elem)
F is F+1,
N1 is N+1,
inv_perm3(X,M,N1).
inv_perm(A,B):-
inv_perm3(A,B,1).
I get false in every input, I test it like this: inv_perm( [2,3,1], X ).
it's way simpler... a hint
?- X=[3,8,5,10,9,4,6,1,7,2],same_length(Y,X),nth1(I,X,V),nth1(V,Y,I).
X = [3, 8, 5, 10, 9, 4, 6, 1, 7|...],
Y = [_358, _364, 1, _376, _382, _388, _394, _400, _406|...],
I = 1,
V = 3 ;
X = [3, 8, 5, 10, 9, 4, 6, 1, 7|...],
Y = [_358, _364, _370, _376, _382, _388, _394, 2, _406|...],
I = 2,
V = 8 ;
...
while I've shown only 1 element of both lists, you should use forall/2 to check all elements, or findall/3 to relate both lists. Findall would allow to generate the inverse, while forall just would check for correctness
The input to this predicate is S, a list of integers I, and a list of integers T. Assumption is that both S and I are fully instantiated, but not T. The function multiplies the the first element in I with the first element in T then sums it with the second element in I multiplied with the [...] you get the idea. However, the struggle I am having is making this function succeed when T isn't instantiated it. I don't want to use use_module(library(clpfd)) even though it may be easier.
I already have something which multiplies correctly, as a helper. To get rid of the "uninstantiated variable" issue I figured I let a variable = S div head of I, so I'd instantiate a new variable, and I'd do this for each item in list I. Then I'd generate all the permutations such that the multiplication of items in a list summed results to S
mult_prod(A, B, S) :-
sum_prod(A, B, 0, S).
mult_prod([A | As], [B | Bs], Accum, S) :-
Accum1 is Accum + A*B,
mult_prod(As, Bs, Accum1, S).
mult_prod([], [], Accum, Accum).
multSum(S, I, T) :-
My naive solution:
multSum(0, [], []).
multSum(S, [I | Is], [T | Ts]) :-
between(0, S, T),
S1 is S - I * T,
multSum(S1, Is, Ts).
Sample output:
?- multSum(42, [6, 7, 8, 9], T).
T = [0, 0, 3, 2] ;
T = [0, 1, 1, 3] ;
T = [0, 6, 0, 0] ;
T = [1, 0, 0, 4] ;
T = [1, 4, 1, 0] ;
T = [2, 2, 2, 0] ;
T = [2, 3, 0, 1] ;
T = [3, 0, 3, 0] ;
T = [3, 1, 1, 1] ;
T = [4, 0, 0, 2] ;
T = [7, 0, 0, 0] ;
false.
Potential problems:
This was just too easy; I must have overlooked something in your question or totally misinterpreted it.
Tested in SWI Prolog only; relies on between/3. If necessary, you can implement this predicate yourself.
Limited to non-negative numbers.
Exhaustively generates all possibilities, which can be quite a lot.
Probably not the most efficient implementation possible...
I just made up a program, doing following task: "Get elements, which values are equal to their indexes".
Here is the code:
% get element's index
get_index([Element|_], Element, 0).
get_index([_|T], Element, Index) :-
get_index(T, Element, Index1),
Index is Index1+1.
index_equals_to_element(List, List2) :-
member(X, List),
get_index(List, X, Index),
Index =:= X,
append([], [X], List2).
It works pretty well. But there is one problem. For list [0, 3, 2, 4, 0] my predicate index_equals_to_element returns [0, 2, 0].
Okay, let it happen. But when I'm trying to output only unique elements, I'm getting the same list without any changes. Example:
?- index_equals_to_element([0, 3, 2, 4, 0], List).
% Outputs [0, 2, 0]
?- sort(List, List2).
% Outputs [0, 2, 0] either, when expected [0, 2]
It's very strange for me, because this works fine:
?- sort([0, 2, 1, 0], List).
% Outputs [0, 1, 2].
Why sort doesn't work only with the list, generated by my predicate?
A simple solution is:
index_equals_to_element(List1, List2) :-
% assume that list position index starts at 0
index_equals_to_element(List1, 0, List2).
index_equals_to_element([], _, []).
index_equals_to_element([X| Xs], Index, List2) :-
NextIndex is Index + 1,
( X == Index ->
List2 = [X| Tail],
index_equals_to_element(Xs, NextIndex, Tail)
; index_equals_to_element(Xs, NextIndex, List2)
).
Example call:
?- index_equals_to_element([0, 3, 2, 4, 0], List).
List = [0, 2].
I suggest you study it by using the trace feature of your Prolog system by typing the query:
?- trace, index_equals_to_element([0, 3, 2, 4, 0], List).
Step trough the execution until is the predicate definition is clear for you.
Your index_equals_to_element([0, 3, 2, 4, 0], List). doesn't output [0, 2, 0] as you claim, but gives three answers [0], [2] and [0]:
?- index_equals_to_element([0, 3, 2, 4, 0], List).
List = [0] ;
List = [2] ;
List = [0] ;
false.
You can use findall to get what you want:
?- findall(X, index_equals_to_element([0, 3, 2, 4, 0], [X]), List).
List = [0, 2, 0].
Update. Here is what I think a better implementation of index_equals_to_element/2:
index_equals_to_element(List, List2) :-
index_equals_to_element(List, 0, List2).
index_equals_to_element([], _, []).
index_equals_to_element([X | Rest], I, Rest2) :-
Inext is I + 1,
index_equals_to_element(Rest, Inext, NewRest),
( X =:= I ->
Rest2 = [X | NewRest]
;
Rest2 = NewRest
).
Test run:
?- index_equals_to_element([0, 3, 2, 4, 0], List).
List = [0, 2].
?- index_equals_to_element([0, 1, 2, 2, 4, 5], List).
List = [0, 1, 2, 4, 5].
The other answers are best for learning the nuts and bolts of Prolog. But here's a more concise (but also easier to grok) solution using the higher-order predicate findall/3 and nth0/3 from the SWI-Prolog library(lists):
elements_equal_to_index(List, Elements) :-
findall(Index, nth0(Index, List, Index), Elements).
Edit:
As #Paulo Moura pointed out in a comment, the above answer is only equivalent to the others offered here if all argument are instantiated. I.e., if the above encounters a free variable in the list, I will bind that variable to its index in the list instead of rejecting it as an unsatisfactory element. The addition of a test for strong equality between the index and the list element should make the answer conform:
elements_equal_to_index(List, Elements) :-
findall( Index,
( nth0(Index, List, Elem),
Elem == Index ),
Elements
).
I'm trying to figure out how to generate a list of sets, where each set has a length of N and the sum of each set is X.
I found this code:
num_split(0,[]).
num_split(N, [X | List]):-
between(1,N,X),
plus(X,Y,N),
num_split(Y,List).
And I can use that to get a list of sets with sum X:
num_split(6,List),length(List,5).
List = [1, 1, 1, 1, 2] ;
List = [1, 1, 1, 2, 1] ;
List = [1, 1, 2, 1, 1] ;
List = [1, 2, 1, 1, 1] ;
List = [2, 1, 1, 1, 1] ;
false.
The problem is that those are all permutations, and I'm looking for combinations. The output I'm looking for should be something like get_combos(Sum,Length,List):
get_combos(6,2,List).
List = [5,1];
List = [4,2];
List = [3,3];
false.
Any pointers?
If you have access to a CLP(FD) library, you can use this code:
:- [library(clpfd)].
get_combos(Sum, Length, List) :-
length(List, Length),
List ins 1 .. Sum,
% all_distinct(List), not really useful here
sum(List, #=, Sum),
chain(List, #<),
label(List).
test:
?- get_combos(10,3,L).
L = [1, 2, 7] ;
L = [1, 3, 6] ;
L = [1, 4, 5] ;
L = [2, 3, 5] ;
Maybe I misunderstood your question. Use this chain
...
chain(List, #=<),
....
to get possible duplicates values:
?- get_combos(10,3,L).
L = [1, 1, 8] ;
L = [1, 2, 7] ;
L = [1, 3, 6] ;
L = [1, 4, 5] ;
L = [2, 2, 6] ;
L = [2, 3, 5] ;
L = [2, 4, 4] ;
L = [3, 3, 4] ;
false.
Enforce an "equal or greater" restriction between successive values in the array.
You can add it on as another predicate:
is_combination([]).
is_combination([_]).
is_combination([A,B|List]) :- A =< B, is_combination([B|List]).
get_combos(Sum, Length, List) :-
num_split(Sum, Length, List),
is_combination(List).
Unfortunately, tacking it on the end of the num_split/3 does not necessarily increase its performance, so adding it directly into the algorithm would be marginally better:
get_combos(_, 0, []).
get_combos(Sum, 1, [Sum]).
get_combos(Sum, Length, [A, B|List]) :-
between(1, Sum, A),
plus(A, NextSum, Sum),
plus(1, NextLength, Length),
get_combos(NextSum, NextLength, [B|List]),
A =< B.
I'm not sure just how much more performance this gets, as the comparison has to be after the recursion, due to the less-than-or-equals operator (=<) requiring both operands to be fully instantiated for it to work.