%mathpiper,def="Count"

Function("Count",{list,element})
[
   Local(result);
   Bind(result,0);
   ForEach(item,list) If(IsEqual(item, element), Bind(result,AddN(result,1)));
   result;
];

%/mathpiper



%mathpiper_docs,name="Count",categories="User Functions;Lists (Operations)"
*CMD Count --- count the number of occurrences of an expression
*STD
*CALL
	Count(list, expr)

*PARMS

{list} -- the list to examine

{expr} -- expression to look for in "list"

*DESC

This command counts the number of times that the expression "expr"
occurs in "list" and returns this number.

*E.G.

In> lst := {a,b,c,b,a};
Result: {a,b,c,b,a};
In> Count(lst, a);
Result: 2;
In> Count(lst, c);
Result: 1;
In> Count(lst, x);
Result: 0;

*SEE Length, Select, Contains
%/mathpiper_docs