I have a prolog homework, which should work like this:
singles([1,2,3,2,2,4,1], [3,4]).
true
Now I’ve figured out I should test if one element is single in the list then put all the single element together..then I wrote down:
singles(L,SL):-findall(X,isSingle(X,L),SL).
isSingle(X,L):-member(X,L),append(Y,[X|Z],L),not(member(X,L1)),append(Y,Z,L1).
in the isSingle function the X should be in List L but not in the new List L1 without X, like 1 is in [2,1,3] but not in [2,3], but sadly the whole thing doesn’t work like I thought 🙁
I think the problem is in the isSingle part, can anyone help me?
Switch the order of
not(member(X,L1))andappend(Y,Z,L1)and your code works. I’m no Prolog expert, so I’m not entirely sure about this, but, going through the trace, it seems to have to do with the subtleties of usingnot. You’re forcing the evaluation ofmemberonL1before it has been unified inappend(Y,Z,L1)bit.