Problem 4: Substitute (100 pts)
Write a procedure substitute
that takes three arguments: a list s
, an old
word, and a new
word. It returns a list with the elements of s
, but with every occurrence of old
replaced by new
, even within sub-lists.
Hint1: The built-in
pair?
predicate returns True if its argument is acons
pair.Hint2: The
=
operator will only let you compare numbers, but usingequal?
oreq?
will let you compare symbols as well as numbers. For more information, check out the SICP Scheme Built-In Procedure Manual.
(define (substitute s old new)
'YOUR-CODE-HERE
)
;;; Tests
; scm> (substitute '(a (a b c) d) 'b 'e)
; (a (a e c) d)