If you haven’t had the chance, you really should pick up a copy of Common Lisp: A gentle introduction to symbolic computation by David Touretzky, in there you will find a series of “templates” for recursion. Consider these like tools in you toolbox.
The following is a presentation of these “recursion templates” in Scheme code with generous examples. Not as clumsy or imprecise as a for loop, they are elegant weapons, from a more civilized era.
Double-Test Tail Recursion
This template is a simple search recursion across a list.
(define (has-odd? lst) (cond ((null? lst) #f) ((odd? (car lst)) #t) (else (has-odd? (cdr lst)))))
As you see, we have a Double-Test at the beginning of the cond, the first tests null, and the second tests for the needle that we are searching for in the haystack that we’ve been passed.
In Touretzsky’s version, the first clause returned nil, I’ve elected to return #f which I think is more correct, as an empty list obviously does not have any odds in it. I prefer a uniformity in return types, even though in this example ‘() is certainly not true, therefore it is functionally equivalent.
Search a list for a value
(define (in-list? needle haystack) (cond ((null? haystack) #f) ((equal? (car haystack) needle) #t) (else (in-list? needle (cdr haystack))))) (in-list? "Hello" '("Hello" "World")) ; #t (in-list? "Hello" '("Goobye" "World")) ; #f
Single Test Augmenting Recursion, or Incrementing Recursion
This is a way to recursively accumulate or increment some value, such as counting.
(define (count-items lst) (cond ((null? lst) 0) (else (+ 1 (count-items (cdr lst))))))
List Consing Recursion
Here we build a list of values as we go along, notice that we are decrementing, so the numbers are in reverse order.
(define (count-down n) (cond ((zero? n) '()) (else (cons n (count-down (- n 1))))))
An alternative to this would be:
(define (count-up n) (let loop ((i 1)) (cond ((> i n) '()) (else (cons i (loop (+ i 1)))))))
Simultaneous Multivariate Recursion
Here we define a function that accepts an ordinal, like 1st, 2nd, 3rd (without the ordinal suffix), and
return that element.
In Touretzky’s book, he reimplements nth, which starts at zero, our function however does not accept 0.
(define (snatch i lst) (cond ((<= i 1) (car lst)) (else (snatch (- i 1) (cdr lst)))))
Here we have two paths, one (number?) which conses the car of lst with the result of a recursive call to nums, or, which simply returns whatever would be returned via the continuation. Notice how the numbers come out in order!
(define (nums lst) (cond ((null? lst) '()) ((number? (car lst)) (cons (car lst) (nums (cdr lst)))) (else (nums (cdr lst)))))
Here we combine the results from two independent recursions, each call has the possibility of generating 2 more recursions and on and on.
(define (fib n) (cond ((equal? n 0) 1) ((equal? n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2))))))
(define (find-number lst) (cond ((number? lst) lst) ((atom? lst) #f) (else (or (find-number (car lst)) (find-number (cdr lst))))))