I'm studying prolog language and i have an issue regarding this problem.
I've already created a program that, given a number N
, returns a list with elements between 0
and N
:
list2val(N,L):- list2val(0,N,L).
list2val(N,N,[N]).
list2val(C,N,[C|T]):-
C<N,
N1 is C+1,
list2val(N1,N,T).
?- list2val(5,X).
X = [0,1,2,3,4,5]
Now i'm trying to give an extension that, given a list, returns a list of lists in which every list is list2val
only if the next number is greater than current number. In this case:
?- newFuction([1,5,2,3,9],L).
L = [[0,1],[0,1,2,],[0,1,2,3]]
My code is this, but somethings is wrong:
array(X):- array(X,_L).
array([],_L).
array([H|[T|Ts]],L1):-
H<T,
list2val(H,L2),
array([T|Ts],[L1|[L2]]).
array([T|Ts],L1).
Maybe could be too much difficult to understand but using a list L = [1,5,2,3,9]
i do those steps:
- check 1<5 so i create a 1 list2val until 1..in this case [0,1]
- check 5<2 i dont create nothing.
- check 2<3 i create list2val of 2 ...[0,1,2]
- and so on...
I don't want use a standard predicates, by implement with standard terms.
List
) consisting of all numbersX
between0
andN
?findall(X, between(0, N, X), List).
– firefrorefiddlearray([9], X)
? – Daniel Lyons