title: Sum, Average of elements and Count the occurence of elements in Lists in Prolog
body: <p>how can I write three predicates in Prolog that do the following things:</p>
  
  <p>1) Define the <code>sum (X, N)</code> predicate, which is true when N is the sum of integers from the list X.</p>
  
  <p>2) Define the <code>avg (X, N)</code> predicate that calculates the arithmetic average of all elements of the list X, where N is the number of elements.</p>
  
  <p>3) Define the predicate called <code>count(X, Y, N)</code>, which is true if the list Y contains N element instances
  X.</p>
  
  <p>Could you give me the examples of them and explain to me why they work the way they do? I know there are dozens of sum and avg predicates here on Stack Overflow, but I can't really understand why they work.</p>