Subject: RE: split/3

From: ROCA Jose <[email-masked]>
Date: Wed, 3 Nov 1999 18:38:36 +0100

And maybe another solution that also produces the output as a list of atoms
and does not contain empty elements:

%split(+Atom,+Atom,-List)

split(A,B,C):-
 atom_chars(A,AC),
 atom_chars(B,BC),
 append(BC,AC,BAC),
 split3(AC,BAC,ABAC),
 split2(AC,ABAC,C).

split3(AC,BAC,NewABAC):-
 append(AC,ABAC,BAC),split3(AC,ABAC,NewABAC).
split3(_,ABAC,ABAC).

split2(_,[],[]).
split2(X,Y,[A|O]):- 
 append(Z,X,ZX),append(ZX,R,Y),atom_chars(A,Z),
 split3(X,R,NewR),
 split2(X,NewR,O).

for example:

?- split(ab,ababxxabaabxxabxx,X).

X = [xx, a, xx, xx] 

Yes
?- split(ab,ababxxabaabxxabxx,X).

X = [xx, a, xx, xx] 

Yes
?- split(a,aaxxaaaxxaxx,X).

X = [xx, xx, xx] 

Yes
?- split(a,axxaaaxxaxx,X).

X = [xx, xx, xx] 

Yes
?- 

> ----------
> From: 	Alfonso Ortega de la Puente
> Sent: 	Wednesday, November 03, 1999 17:41
> To: 	'Luis Iraola Moreno'; Joakim Vrblom
> Cc: 	[email-masked]
> Subject: 	RE: split/3
> 
> The predicate from Luis Iraola seems to be useful...but it changes the
> type of the arguments that Joakim 
> handles.
> 
> It could be useful to define the following transformation predicate:
> 
> 
> list_chars_list_atoms([], []).
> list_chars_list_atoms([A|B], [C|D]) :-
>         atom_char(C, A),
>         list_chars_list_atoms(B, D).
> 
> And use it like this:
> 
> atom_chars(axaxxa,L),list_chars_list_atoms(L,LA).
> 
> L = [97, 120, 97, 120, 120, 97]
> LA = [a, x, a, x, x, a] 
> 
> In order to use then the predicate from Luis Iraola
> 
> segment(LA, [a], List).
> 
> 
> 
> 
> -----Mensaje original-----
> De:	Luis Iraola Moreno [SMTP:[email-masked]]
> Enviado el:	miércoles 3 de noviembre de 1999 13:39
> Para:	Joakim Örblom
> CC:	[email-masked]
> Asunto:	Re: split/3
> 
> 
> 
> 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] ] 
> 
> 
> 
> 
> ----------------
> * To UNSUBSCRIBE, please use the HTML form at
> 
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/index.html#mailinglist
> 
> or send mail to [email-masked] using the Subject:
> "unsubscribe"
> (without the quotes) and *no* message body.
> 
> ** An ARCHIVE of this list is maintained at
> 
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
> 
> 
> 
> ----------------
> * To UNSUBSCRIBE, please use the HTML form at
> 
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/index.html#mailinglist
> 
> or send mail to [email-masked] using the Subject:
> "unsubscribe"
> (without the quotes) and *no* message body.
> 
> ** An ARCHIVE of this list is maintained at
> 
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
>