t3x.org / nss / count.html

(Nils' Scheme Snippets)

 
Paren matching: OFF  |  Category: lists  |  Overview  |  Scheme Books  |  License
 

(count pair) => integer

 
Purpose
Count the atomic members of a pair.
 
Arguments
x list to count
 
Example
(count '(a (b (c)) d . e)) => 5
(define (count x)
  (cond ((null? x) 0)
        ((pair? x)
          (+ (count (car x))
             (count (cdr x))))
        (else 1)))

Copyright (C) 2007 Nils M Holm <nmh @ t3x . org>