Some typical flat recursion patterns
Do all list elements satisfy a predicate?
; are-all? ; takes a list, returns #t if all list elements satisfy pred? ; returns #f otherwise (define (are-all? lst) (cond [(null? lst) #t] [(pred? (car lst)) (are-all? (cdr lst))] [else #f]))
where pred? is a placeholder for some predicate.
For example, num?, symbol?, and pair?
are possible instantiations of pred?.
Why are there three branches in the cond here? Doesn't
the definition of a list have just two alternatives?
Does at least one list element satisfy a predicate?
; at-least-one? ; takes a list, returns #t if there exists a list element ; that satisfies pred? ; returns #f otherwise (define (at-least-one? lst) (cond [(null? lst) #f] [(pred? (car lst)) #t] [else (at-least-one? (cdr lst))]))