Chapter 2, Building Abstractions with Data

Section - 2.2 - Hierarchical Data and the Closure Property

Exercise 2.17


Note that we need to return the last pair, not the last element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#lang sicp

(#%require (only racket/base error))

(define (last-pair list)
  (if (null? list)
      (error "Error: Empty list")
      (let ((tail (cdr list)))
          (if (null? tail)
              list
              (last-pair (cdr list))
          )
      )
  )    
)