[1, 2, a, [1,2], B, 1, [1, 2, 3]]
[H|T]
% mathematical model:
% process(l1l2…ln) = { [], if L = Ø
% { process(l2…ln), if is_list(l1)
% { l1(l2…ln), otherwise
% process(L-list, R-list)
% flow model: (i, o)
process([], []).
process([H|T], R):- is_list(H), !,
process(T,R).
process([H|T], [H|R]):- %\\+ is_list(H), %not(is_list(H)) %atomic(H) - no condition needed because of the cut above
process(T,R).
% mathematical model:
% substitute(l1l2…ln) = { [], if L = Ø
% { l11 U substitute(l2…ln), if is_list(l1) and sum(l1) % 2 == 1
% { l1 U substitute(l2…ln), otherwise
% substitute(L-list, R-list)
% flow model: (i, o)
substitute([], []).
substitute([H|T], [H1|TR]):- is_list(H),
sum(H, S),
S mod 2 =:= 1, !,
H = [H1|_]
substitute(T, TR).
substitute([H|T],[H|TR]):- substitute(T, TR).
% mathematical model:
% mountain(l1l2…ln, AF) = { true, if n <= 1 and AF = 1
% { false, if n <= 1 and AF = 0
% { mountain(l2…ln, 0), if l1 < l2, AF = 0
% { mountain(l2…ln, 1), if l1 > l2, AF = 0
% { mountain(l2…ln, 1), if l1 < l2, AF = 1
% { false, otherwise
% main_mountain(l1...ln) = { isMountain(l1l2...ln, 0), l1<l2
% { false, otherwise
% mountain(L-list, AF-aspect flag)
% flow model: (i, o)
% main_mountain(L-list)
% flow model: (i)
%clauses
mountain([_], 1).
mountain([H1, H2|T], 0):- H1 < H2,
mountain([H2|T], 0).
mountain([H1, H2|T], 0):- H1 > H2,
mountain([H2|T], 1).
mountain([H1, H2|T], 1):- H1 < H2,
mountain([H2|T], 1).
% mountain([H1, H2|T], _):- H1 > H2,
% mountain([H2|T], 1).
% mathematical model:
% remove(l1l2…ln) = { [], if L = Ø
% { l1 U remove(l2…ln), if l1 is even
% { remove(l2…ln), if l1 is odd
% remove(L-list, R-list)
% L – linear list from which we remove odd numbers
% R – the resulting list
% flow model: (i, o)
remove([], []).
remove([H|T], [H|TR]):- H mod 2 =:= 0, !,
remove(T, TR).
remove([H|T], TR):- % H mod 2 =:= 1,
remove(T, TR).
% mathematical model:
% process(l1l2…ln) = { [], if L = Ø
% { remove(l1l2…ln) U process(l2…ln), if l1 is a list, l11 < l12, mountain(l1, 0)
% { l1 U process(l2…ln), otherwise
% process(L-list, R-list)
% L – the initial heterogeneous list
% R – the resulting list
% flow model (i, o)
process([], []).
process([H|T], [HR|TR]):- is_list(H),
H = [A, B|_],
A < B,
mountain(H, 0),!,
remove(H, HR),
process(T, TR).
process([H|T], [H|TR]):- process(T, TR).
% predicates:
% remove(L:list, R:list)
%
% flow model:
% (i, o) or (i, i)
%
% domains:
% L - the initial list
% R - resulting list
%
% mathematical model:
% remove([l1..ln]) = { [], n = 0
% { [], n = 2 and l1 < l2
% { remove([l2 l3..ln]), if l1 < l2 < l3
% { remove([l3..ln]), l1 < l2 > l3
% { l1 U remove([l2..ln]), otherwise
remove([], []).
remove([H1,H2], []):- H2 > H1.
remove([H1,H2,H3|T], R):- H2 > H1,
H3 > H2+1, !,
remove([H2,H3|T], R).
remove([H1,H2,H3|T], R):- H2 > H1,
not(H3 <= H2), !,
remove([H3|T], R).
remove([H|T],[H|R]):- remove(T, R).
process([], []).
process([H|T], [HR|TR]):- is_list(H),
remove(H, HR), !,
process(T, TR).
process([H|T],[H|TR]):- process(T, TR).
% Another approach:
%
% mathematical model:
% eat(l1, [l2...ln]) = { [l3..ln]
%
% packman([l1..ln]) = { [], n = 0
% { packman(eat(l1, [l2..ln])), if l1 < l2
% { l1 U packman([l2..ln]), otherwise
%
% examples:
% packman([4,1,2,5], R) => R = [] or [4, 5] ?.
% packman([1,2,3,4,5,2,3,4,5,6,7,9,11], R) => R = [9, 11].
% packman([3,2,1,2,3,4,5,2,3,4,5,6,7,9,11], R) => R = [3, 2, 9, 11].
% packman([1, 2, 4, 3, 4, 5, 7, 9], R)
%
% clauses:
eat(H1, [H2|T], R) :- H2 =:= H1+1, !,
eat(H2, T, R).
eat(_,T,T).
packman([], []).
packman([H1,H2|T], R):- H2 =:= H1+1, !,
eat(H1, [H2|T], ER),
remove(ER, R).
packman([H|T],[H|RT]):- remove(T,RT).