Contents    Page-10    Prev    Next    Page+10    Index   

Recursive Processing of List

Recursive processing of a list is based on a base case (often an empty list), which usually has a simple answer, and a recursive case, whose answer is based on a recursive call to the same function on the rest of the list.

As an example, we can recursively find the number of elements in a list. (The Clojure function that does this is called count.)


(defn length [lst]
  (if (empty? lst)       ; test for base case
      0                  ; answer for base case
      (+ 1
         (length (rest lst))) ) )  ; recursive call

Note that we are using empty? to test for an empty list; this is because Clojure could represent the end of a list in different ways.