title: Average using &rest in lisp
body: <p>So i was asked to do a function i LISP that calculates the average of any given numbers. The way i was asked to do this was by using the &amp;rest parameter. so i came up with this :</p>
  
  <pre><code>(defun average (a &amp;rest b)
     (cond ((null a) nil)
           ((null b) a)
           (t (+ (car b) (average a (cdr b))))))
  </code></pre>
  
  <p>Now i know this is incorrect because the (cdr b) returns a list with a list inside so when i do (car b) it never returns an atom and so it never adds (+)</p>
  
  <p>And that is my first question:</p>
  
  <ul>
  <li>How can i call the CDR of a &amp;rest parameter and get only one list instead of a list inside a list ?</li>
  </ul>
  
  <p>Now there is other thing : 
  When i run this function and give values to the &amp;rest, say (average 1 2 3 4 5) it gives me stackoverflow error. I traced the funcion and i saw that it was stuck in a loop, always calling the function with the (cdr b) witch is null and so it loops there. 
  My question is:</p>
  
  <ul>
  <li>If i have a stopping condition: ( (null b) a) , shouldnt the program stop when b is null and add "a" to the + operation ? why does it start an infinite loop ? </li>
  </ul>
  
  <p>EDIT: I know the function only does the + operation, i know i have to divide by the length of the b list + 1, but since i got this error i'd like to solve it first.</p>