Laboratory Exercises For Computer Science 151

Flat Recursion

Flat Recursion

In their text, Scheme and the Art of Programming, Springer and Friedman state:

In our examples of recursion involving lists, we made the recursive step by applying the procedure to the cdr of the list. The car of the list was then treated as a unit, which is why the recursion was over the top-level items in the list. We refer to a recursion over the top-level items of a list as a flat recursion, and we say that the procedure so defined is flatly recursive or simply a flat procedure.

Goals: This laboratory procedure reviews flat recursion, clarifying this general approach to problem solving, considering a few more examples, and applying flat recursion to the solution of a few more problems.

Problems For This Lab:

  1. Describe what the following procedure does:
    
    (define mystery
        (lambda (L)
            (cond ((null? L) 0)
                  ((number? (car L)) (+ 1 (mystery (cdr L))))
                  (else (mystery (cdr L)))
            )
        )
    )
    
    Hint: Look at several examples, such as (mystery '(7 a 1/2 b 3.14159 c -9)).
  2. Describe what the following procedure does:
    
    (define question-2
        (lambda (L)
            (cond ((null? L) #t)
                  ((not (number? (car L))) #f) 
                  (else (and (odd? (car L)) (question-2 (cdr L))))
            )
        )
    )
    
    Hint: Again, look at several examples, such as
    (question-2 '(7 a 1/2 b 3.14159 c -9))
    (question-2 '(1 3 5 7 9))
    (question-2 '(1 3 6 7 9))

  3. Write a procedure to-zero that changes all numbers in a list to 0 but leaves all other elements on the list unchanged. For example, (to-zero '(7 a 1/2 b 3.14159 c -9)) should return (0 a 0 b 0 c 0).

  4. Write a procedure check-odd that has a list ls of numbers as a parameter and that returns a list indicating whether the corresponding number on ls is odd. For example, (check-odd '(1 2 3 4 5 6 7 8 9 10)) should return (#t #f #t #f #t #f #t #f #t #f) .

  5. Write a procedure substitute that has three parameters, old, new, and ls and that returns a new list from ls with all occurences of old changed to new. Some examples follow:

    
    (substitute 'the 'a '(the dog lives in the house))
       ===> (a dog lives in a house)
    (substitute 8 9 '(1 2 3 4 5 6 7 8 9)) 
       ===> (1 2 3 4 5 6 7 9 9)
    (substitute 0 9 '(1 2 3 4 5 6 7 8 9)) 
       ===> (1 2 3 4 5 6 7 8 9)
    (substitute 'a 'b '()) 
       ===> ()
    
  6. Write a procedure count-out-of-order that has a list of numbers as parameter and that returns the number of consecutive values that are out of ascending order in the list. In this problem, a pair of values are out of order if the second is smaller than the first, but equal values are not considered out of order. Here are some examples.

    
    (count-out-of-order '())
       ===> 0
    (count-out-of-order '(1 2 3 4 5 6 7 8 9 10))
       ===> 0
    (count-out-of-order '(1 1 1 1 1 1 1 1 1 1))
       ===> 0
    (count-out-of-order '(2 1))
       ===> 1
    (count-out-of-order '(3 2 1))
       ===> 2
    (count-out-of-order '(3 2 1 2 3))
       ===> 2
    
  7. Write a procedure insert that has two parameters, item and a list ls, and that inserts the item between every adjacent pair of items in ls . Some examples follow:

    
    (insert 'here '())
        ===> ()             ; no adjacent items
    (insert  'here '(there))
        ===> (there)        ; no adjacent items
    (insert '0 '(1 2 3 4 5))
        ===> '(1 0 2 0 3 0 4 0 5)
    (insert '() '(this is a list))
        ===> (this () is () a () list)
    


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/151.fa00/lab-flat-recursion.html

created February 12, 1997
last revised September 20, 2000