Check if all numbers in a list are different in prolog

后端 未结 7 1478
失恋的感觉
失恋的感觉 2021-01-18 10:02

I want to create a rule in prolog that checks if there\'s a repeated number in a list.

For example:

  • for [1,2,3,4] it will return tru
7条回答
  •  孤城傲影
    2021-01-18 10:48

    This isn't very efficient, but for each number you can check if it appears again later. Like so:

    Different([H|T]):-
      CheckSingle(H, [T]),
      Different([T]).
    
    Checksingle(_,[]).
    
    Checksingle(Elem, [H, T]):-
      Elem != H,
      Checksingle(Elem, [T]).
    

提交回复
热议问题