Subject: Re: split/3

From: Luis Iraola Moreno <[email-masked]>
Date: Wed, 03 Nov 1999 12:39:08 +0000 (WET)

On Wed, 3 Nov 1999, Joakim Örblom wrote:

> Help! How do I construct a predicate split/3?? E.g. 
> split(a,axxaxxaxx,[xx,xx,xx]).
> Thanx from Carl Orblom ([email-masked])

This predicate may do the job:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% segment(InputList, SegmentSeparator, SegmentList)
%

segment([], _, []).

segment([X|Y], [H|T], [Segment|Tail]):-
	append(Segment,[H|U], [X|Y]),
	append(T, Rest, U),
	!,
	segment(Rest, [H|T], Tail).

segment([X|Y], _, [[X|Y]]).

Example:

?- segment([a,x,x,a,xx],[a],L).

L=[ [], [x,x], [xx] ]