Prolog: why my predicate returns false?

前端 未结 2 1167
日久生厌
日久生厌 2021-01-16 07:33

so I wrote a predicate that counts how many times an element occurs in a list of lists.

count([], _, 0).                                  #base case

count([         


        
2条回答
  •  遥遥无期
    2021-01-16 08:16

    Ok, it looks like it was backtracking on the part where 'Elem is not the head of sublist', and I was able to fix it by changing it to:

    count([[_|Rest]|OtherLists], Elem, Count) :-      #Elem is not the head of sublist
        !,
        count([Rest|OtherLists], Elem, Count).
    

    If anyone can confirm whether this is a correct solution. Thanks

提交回复
热议问题