Define a relation first_last(L1, L2) which takes a list L1 and returns a list L2 containing the first three and last three elements of L1

Viewed 69

Basically I planned on getting the first three elements and also the last three and then concatenate them together. This has been a hassle. I know something like this first([A, B, C|_]) can help me get the first three. apart from this I am completely stuck. I am a noob at prolog, please help. Given a relation first_last(L1, L2), when I write something like first_last([1,2,3,4,5,6,7,8,9], L2) . where L2 is the resulting list, i should get L2 = [1,2,3,7,8,9]

1 Answers
first_last(Xs, [A,B,C,X,Y,Z]) :-
   phrase(( [A,B,C], ..., [X,Y,Z] ), Xs).

... --> [] | [_], ... .

Related