Sum of list (or matrix) elements transformed by a function

### 6.3.25 Sum of list (or matrix) elements transformed by a function

The count command adds the values of a function applied to the elements of a list or matrix.

- count takes two arguments:
  - f, a real-valued or boolean-valued function.
  - L, a list or a matrix.
- count(f,L) returns the sum of f(x) for all elements x in L.

  If f is a boolean-valued function, this is just the number of elements for which the boolean is true.

#### Examples

count((x)->x,[2,12,45,3,7,78])

147 |  |  |  |  |  |  |  |  |  |

because 2+12+45+3+7+78=147.

count((x)->x<12,[2,12,45,3,7,78])

3 |  |  |  |  |  |  |  |  |  |

count((x)->x==12,[2,12,45,3,7,78])

1 |  |  |  |  |  |  |  |  |  |

count((x)->x>12,[2,12,45,3,7,78])

2 |  |  |  |  |  |  |  |  |  |

count(x->x^2,[3,5,1])

35 |  |  |  |  |  |  |  |  |  |

Indeed, 32+52+11=35.

count(id,[3,5,1])

9 |  |  |  |  |  |  |  |  |  |

Indeed, id is the identity functions and 3+5+1=9.

count(1,[3,5,1])

3 |  |  |  |  |  |  |  |  |  |

Indeed, 1 is the constant function equal to 1 and 1+1+1=3.