; CSC151, Class 29: Deep Recrusion
;
; Overview:
; * List Recursion, Revisited
; * Deep Recursion
; * Problem: count-elements
; * Problem: depth
;
; Notes:
; * Reading for tomorrow: Higher-order procedures
; * Reese wrote notes for Friday's class.
; * Questions on vectors?
; * First result from conference: I'll work on guidelines
; for style.
; * Second result from conference: Temple Burling story.
; * Third result from conference: A "fun" exercise.
; * Sorry about the last homework.
; * Design of today's class
; Start with something we should know: Recursion over lists.
; (1) Is the list null?
; (2) If so, return base case
; (3) Do something on the car and recurse on cdr.
; Sometimes we check if there's only one thing, rather than
; no things in the list.
; If we're adding all the elements and there's only one
; thing, use that thing.
; Repeated addition, subtraction, etc. may always need
; *two* elements.
; To check for only one element, use (null? (cdr lst))
; To check for no elements, use null?
; Find the length of a list
(define llength
(lambda (lst)
(if (null? lst)
0
(+ 1 (llength (cdr lst))))))
;;; Practica:
;;; > (count-values (list (list 1 2 3) 4 5 6 (list 7 8 9)))
;;; 9
;;; > (count-values (list 1 2))
(define count-values
(lambda (lst)
(cond
((null? lst) 0)
((list? (car lst))
(+ (count-values (car lst)) (count-values (cdr lst))))
(else
(+ 1 (count-values (cdr lst)))))))