Re: List decomposition.



ddtl wrote:
Hello.

Given a list [1,2,...,n] I want to get elements between 1 and n,
that is: 2,3,...n-1.

Hold that thought.

In other words,

inner_member(L,X) if and only if

L = [LeftmostTerm] + MiddleTerms + [RightmostTerm]

and

MiddleTerms = LeftofX + [X] + RightOfX .

i.e.,

inner_member(L,X) :-
/* L = [LeftmostTerm] + MiddleTerms + [RightmostTerm] */
append3([LeftMostTerm],MiddleTerms,[RightmostTerm],L),
/* MiddleTerms = LeftOfX + [X] + RightOfX */
append3(LeftOfX,[X],RightOfX,MiddleTerms).

which, after removing commentary and singleton variables, becomes

inner_member(L,X) :-
append3([_],MiddleTerms,[_],L),
append3(_,[X],_,MiddleTerms).

where

append3([],L2,L3,L) :-
append(L2,L3,L).
append3([X|L1],L2,L3,[X|L]) :-
append3(L1,L2,L3,L).

It remains to be proved that the part of the problem specification that
stipulates the middle terms shall appear in the same order as they occur
in the given list is satisfied (if it is). Also, of course, there are
undoubtedly much more efficient ways of defining 'inner_member/2'. The
only point I am trying to make here is that your initial understanding
of the problem was closer to being executable Prolog than you seem to me
to have realized. The key (I think): Prolog gives you complete freedom
to invent "verbs" (predicates) and "pronouns" (variables) to suit your
purposes. Use it!

--
billh
.