Prolog element is a list member check

后端 未结 2 463
旧时难觅i
旧时难觅i 2021-01-27 08:14

I have a little rule in prolog that has to check if an element is a member of a list and write it\'s position in the list,but it only works if the elemebt i\'m looking for is in

2条回答
  •  余生分开走
    2021-01-27 09:15

    Since you report it's working only for the first position, here is the simpler correction possible to your code to make it work in other cases:

    write_element(X,[X|_],1).
    write_element(X,[_|Tail],N):-
      write_element(X,Tail,N1),
      N is N1 + 1.
    

    but the answer from Daniel (+1) is what you should study.

提交回复
热议问题