Giththan
Giththan

Reputation: 58

Split the list element in prolog

I have a problem in split prolog list element.I have some list of atom.If the atom is find in my prolog list then it is split by two words.
My code.

list-->[acar]|[avan]|[ajeep]|[abicycle].

%expected answer
?- split([i,have,acar],L2).
L2=[i,have,a,car];

?- split([there,is,avan,and,acar],L2).
L2=[there,is,a,van,and,a,car];

I couldn't get better way.Any idea to solve this problem.Thank you...

Upvotes: 1

Views: 288

Answers (1)

Gamsh
Gamsh

Reputation: 545

Refer this:

list(car)-->[acar].
list(van)-->[avan].
list(jeep)-->[ajeep].
list(bicycle)-->[abicycle].

split(In,X):-list(W,[L],[]),append(A,[L|T],In),append(A,[a,W|T],X1),split(X1,X),!.
split(L,L).

Edit

split_a(X):-list([L],[]),atom_length(L,S),N is S-1,sub_atom(L,1,N,_,X).

Upvotes: 1

Related Questions