;Counts the number of right and left parenthesis in ;a given list. ;Recurses down each sub-list of the argument list, ls ;returns 2 for each () ;returns (count-parens (car ls)) + ; (count-parens (cdr ls) for each list. (define count-parens-all (lambda (ls) ;(display ls) (newline) (cond ((not (list? ls)) '("Error: argument must be a list")) ((null? ls) 2) (else (cond ((atom? (car ls)) (count-parens-all (cdr ls)) ) (else (+ (count-parens-all (car ls)) (count-parens-all (cdr ls)) ) )))))) ;============== Unit Tests ====================== (load "atom.scm") (load "test.scm") (load "writeln.scm") (define msg "count-parens-all") (test msg (count-parens-all '()) '2) (test msg (count-parens-all '((a b) c)) '4) (test msg (count-parens-all '(((a () b) c) () ((d) e))) '14) (test msg (count-parens-all '((((a))))) '8) (test msg (count-parens-all 'a) '("Error: argument must be a list"))