Write a procedure substitute that takes three arguments: a list, an old word, and a new word. It should return a copy of the list, but with every occurrence of the old word replaced by the new word, even in sublists. For example:
> (substitute ’((lead guitar) (bass guitar) (rhythm guitar) drums) ’guitar ’axe)
((lead axe) (bass axe) (rhythm axe) drums)
The argument "l" is the list that would be evaluated, I just don't know what name is appropriate because the word "list" is already a procedure. Subl will evaluate the items that are lists.
Please review my code.
(define (substitute l old new) (if (null? l) '() (let ((head (car l)) (tail (cdr l)) (subl (lambda (x) (substitute x old new)))) (cond ((list? head) (cons (subl head) (subl tail))) ((equal? head old) (cons new (subl tail))) (else (cons head (subl tail)))))))
How is my use of let and lambda? Did I manage data abstraction correctly? How can I make this code better and faster?