如何为以下输出编写规则?
?- atoms_and_integers([a,b,1,2])
输出:
Number of Atoms:2
Number of integers:2
答案 0 :(得分:1)
我知道这对你的任务没什么帮助,但我是图书馆的粉丝(aggregate)!
atoms_and_integers(L, NAs, NIs) :-
aggregate(count, E^(member(E, L), atom(E)), NAs),
aggregate(count, E^(member(E, L), integer(E)), NIs).
答案 1 :(得分:0)
假设这不是家庭作业,在这种情况下,我不想成为犯罪的伙伴,并且会说以下代码可以被任何人用于除作弊之外的任何目的:-)
atoms_and_integers(L) :-
atoms_and_integers_aux(L, Atoms, Integers),
writeln(['Atoms: ', Atoms]),
writeln(['Integers: ', Integers]).
atoms_and_integers_aux([], 0, 0).
atoms_and_integers_aux([H|T], Atoms, Integers) :-
atom(H), !,
atoms_and_integers_aux(T, AtomsT, Integers),
Atoms is AtomsT + 1.
atoms_and_integers_aux([H|T], Atoms, Integers) :-
integer(H), !,
atoms_and_integers_aux(T, Atoms, IntegersT),
Integers is IntegersT + 1.
atoms_and_integers_aux([_H|T], Atoms, Integers) :-
atoms_and_integers_aux(T, Atoms, Integers).