;; The original length (define (len l) (if (null? l) 0 (+ 1 (len (cdr l))))) ;; Length using map (define (len1 l) (apply + (map (lambda (x) 1) l))) ;; Atomcount from last time... (define (atom? x) (not (pair? x))) (define (atomcount x) (cond ((null? x) 0) ((atom? x) 1) (else (+ (atomcount (car x)) (atomcount (cdr x)))))) ;; Atomcount using map and apply (define (atomcount2 x) (cond ((atom? x) 1) (else (apply + (map atomcount2 x))))) > (atomcount2 '(3 (4 5))) 3 (define (foldr f l base) (if (null? l) base (f (car l) (foldr f (cdr l) base)))) ;; Length using fold (define (len2 l) (foldr (lambda (x y) (+ 1 y)) l 0)) ;; Atomcount using fold (define (atomcount4 x) (if (atom? x) 1 (foldr (lambda (x y) (+ (atomcount4 x) y)) x 0))) ;; Another atomcount using fold -- this one ;; isn't exactly the same as atomcount4 -- how it is ;; different? (define (atomcount5 x) (foldr (lambda (x y) (+ (if (atom? x) 1 (atomcount5 x)) y)) x 0))