Chapter 2, Building Abstractions with Data

Section - 2.2 - Hierarchical Data and the Closure Property

Exercise 2.27


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#lang sicp

(define (reverse list)
  (define (reverse-iter rs ls)
      (if (null? ls)
          rs
          (reverse-iter
                   (cons
                        (let ((data (car ls)))
                           (if (pair? data)
                                  (reverse data)
                                  data
                           )
                        )  
                        rs
                   )
                   (cdr ls)
          )
      )
  )
  (reverse-iter nil list)
)

Output:

1
2
3
4
5
6
> (define x (list (list 1 2) (list 3 4)))
> (reverse x)
(mcons (mcons 4 (mcons 3 '())) (mcons (mcons 2 (mcons 1 '())) '()))
> (define x (list 1 (list 2 3) 4 5 (list 6 7) 8))
> (reverse x)
(mcons 8 (mcons (mcons 7 (mcons 6 '())) (mcons 5 (mcons 4 (mcons (mcons 3 (mcons 2 '())) (mcons 1 '()))))))