I need to filter the list [#,d,e,#,f,g] such that I get the output as [[d,e],[f,g]] ,
I am stuck while creating a new list every time I encount
Here is another version, which uses an even more general approach:
list_splitbyhash(Xs, Xss) :-
phrase(by_split(=(#), Xss), Xs).
=(X,X,true).
=(X,Y,false) :- dif(X,Y).
by_split(_C_2, []) --> [].
by_split(C_2, Xss) -->
[E],
{call(C_2,E,T)},
( { T = true },
by_split(C_2, Xss)
| { T = false, Xss = [[E|Xs]|Xss1] },
all_seq(callfalse(C_2),Xs),
el_or_nothing(C_2),
by_split(C_2, Xss1)
).
callfalse(C_2,E) :-
call(C_2,E,false).
el_or_nothing(_) -->
call(nil).
el_or_nothing(C_2), [E] -->
[E],
{call(C_2,E,true)}.
nil([], []).
With lambdas, this can be expressed more compactly. Instead of
all_seq(callfalse(C_2),Xs)
and the definition for callfalse/3, one can now write
all_seq(C_2+\F^call(C_2,F,false))