filter list into separate lists

前端 未结 3 695
刺人心
刺人心 2020-12-21 08:53

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

3条回答
  •  星月不相逢
    2020-12-21 09:07

    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))
    

提交回复
热议问题