如果项目不在列表(prolog)中,则返回false
以下是如果在那里,则从列表中删除键(a)的代码。如果不存在,它当前将返回整个列表。我希望它能返回“ false”。样本输出也将低于下方。
mySelect(_, [], []).
mySelect(X, [Y|K], [Y|M]):- mySelect(X, K, M), (X \= Y).
mySelect(X, [X|K], R) :- mySelect(X, K, R).
目前,这将输出:
?- my_delete(c,[a,b,c,d],R).
R = [a, b, d] .
?- my_delete(e,[a,b,c,d],R).
R = [a, b, c, d] .
我希望它输出:
?- my_delete(c,[a,b,c,d],R).
R = [a, b, d] .
?- my_delete(e,[a,b,c,d],R).
false .
任何指针都将不胜感激!
Below is the code to remove a key (A) from a list if it is there. If it isn't there, it currently returns the entire list. I would like it instead to return 'false.' Sample outputs will be below as well.
mySelect(_, [], []).
mySelect(X, [Y|K], [Y|M]):- mySelect(X, K, M), (X \= Y).
mySelect(X, [X|K], R) :- mySelect(X, K, R).
Currently this will output:
?- my_delete(c,[a,b,c,d],R).
R = [a, b, d] .
?- my_delete(e,[a,b,c,d],R).
R = [a, b, c, d] .
I would like it to output:
?- my_delete(c,[a,b,c,d],R).
R = [a, b, d] .
?- my_delete(e,[a,b,c,d],R).
false .
Any pointers would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加另一个规则,该规则首先确保X是L的成员,然后再应用
MySelect
。You can add another rule which first ensures that X is a member of L, before applying
mySelect
.将列表分为匹配和非匹配:
或计数项目数,并测试计数大于零:
Either partition the list into matches and non-matches:
or count the number of items and test the count for being greater than zero: