Subset function in prolog

做~自己de王妃 提交于 2020-01-05 08:44:50

问题


I am working on writing a subset function and I have succeeded in doing so. Here's my function which implements member function:

 member( X, [ X | T ] ).
 member( X, [ _ | T ] ) :- member( X, T ).

 subset([], _).
 subset([H|T1], T2) :-
    member(H, T2),
    subset(T1, T2). 
 subset([H1|T1], [H2|T2]) :-
    \+ member(H1, T2),
    subset([H1|T1], T2).

My question is, is there a better way to write this function using the member function of course.


回答1:


The third clause of subset/3 does not make sense and i think that it should be removed. If H1 is not a member of T2, then the recursive call subset([H1|T1], T2) will obviously don't succeed either.

Aside from that, the first two clauses seem the way to go.



来源:https://stackoverflow.com/questions/13938813/subset-function-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!