Prolog, X before Y in a List

前端 未结 4 909
醉话见心
醉话见心 2021-01-21 19:57

I am having trouble understanding prolog, I have to find if X is before Y in a list.

so I have a base case with an empty list

before(X, Y, [ ]).
<         


        
4条回答
  •  遇见更好的自我
    2021-01-21 20:11

    You can use append/3 to locate X and the remaining list after it, then locate Y.

    before(X, Y, L):-
      append(_, [X|Tail], L),
      append(_, [Y|_], Tail).
    

提交回复
热议问题