简体   繁体   English

在Prolog中实现select / 3

[英]Implementing select/3 in Prolog

I'm new to Prolog and came across this predicate, select/3 . 我是Prolog的新手,遇到了这个谓词select/3 I figured how it works though I'm uncertain how I would implement it myself. 尽管我不确定自己将如何实现它,但我知道了它是如何工作的。 I think something like: 我认为是这样的:

selec(El,[El|T],T).
selec(El,[H|T],[H|S]) :-
      selec(El,T,S).
select(El,[],[]).

I know something is wrong. 我知道出事了 My solution only removes the first occurrence. 我的解决方案仅删除第一次出现的情况。 I want it to remove, all occurrences at some point, just like select/3 does. 我希望它在某些时候删除所有出现的内容,就像select/3一样。 Any ideas? 有任何想法吗?

Your code is perfectly fine, except you don't need the select(El,[],[]). 您的代码非常好,除非您不需要select(El,[],[]). predicate. 谓语。

This is all you need: 这就是您所需要的:

selec(El,[El|T],T).
selec(El,[H|T],[H|S]) :-
      selec(El,T,S).

Do keep in mind that the standard prolog parameter order is inputs followed by outputs, so you really should write it like this: 请记住,标准的prolog参数顺序是输入,然后是输出,因此您应该这样写:

selec([El|T],El,T).
selec([H|T],El,[H|S]) :-
      selec(T,El,S).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM