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 a cons
pair.
Hint2: The =
operator will only let you compare numbers, but using equal?
or eq?
will let you compare symbols as well as numbers. For more information, check out the Scheme Built-in Procedure Reference.
(define (substitute s old new)
'YOUR-CODE-HERE
)
;;; Tests
; scm> (substitute '(a (a b c) d) 'b 'e)
; (a (a e c) d)