Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地

一个供Prolog爱好者学习与交流的地方


您没有登录。 请登录注册

计算List中某原子的个数---countif谓词

向下  留言 [第1页/共1页]

1计算List中某原子的个数---countif谓词 Empty 计算List中某原子的个数---countif谓词 周一 三月 26, 2012 2:20 pm

Mercury Liao


Admin

countif(+List, ?Atom, ?Count)

计算List中原子Atom的个数Count

代码:
countif(List,Atom,Count):- (var(Count) -> ! ; true), bagof(I,nth1(I,List,Atom),R), length(R,Count).
countif(List,Atom,Count):- Count = 0.


例:

?- countif([atm, 123, atm, aabbcc], atm, Num).
传回 Num = 2.

?- countif([atm, 123, atm, aabbcc], atmzzz, Num).
传回 Num = 0.

?- countif([atm, 123, atm, aabbcc], String, 1).
传回
String = 123 ;
String = aabbcc ;
false.

?- countif([atm, 123, atm, aabbcc], String, Num).
传回
Str = 123,
Num = 1 ;
Str = aabbcc,
Num = 1 ;
Str = atm,
Num = 2.


适用的Prolog版本:SWI-Prolog

http://prolog.longluntan.net

返回页首  留言 [第1页/共1页]

您在这个论坛的权限:
不能在这个论坛回复主题