count([],0).
count([_|Tail], N) :- count(Tail, N1), N is N1 + 1.
This count all the elements, but I need to count only the numbers.
count([],0).
count([_|Tail], N) :- count(Tail, N1), N is N1 + 1.
This count all the elements, but I need to count only the numbers.
Prolog has an ISO builtin predicate number/1
that checks whether the given parameter is a number.
We can simply use an if-then-else statement that either increments N is N1+1
, or sets N = N1
, like:
count([],0).
count([H|Tail], N) :-
count(Tail, N1),
( number(H)
-> N is N1 + 1
; N = N1
).
You could use number/1
iso built-in predicate:
count([],0).
count([H|Tail], N) :- number(H),count(Tail, N1), N is N1 + 1.
count([H|Tail], N) :- \+number(H),count(Tail, N).