This is sort of a follow-up to Replacing elements from a list and its sublists but now there are arbitrary numbers of words that would be replaced stored in a list.
Now write substitute2 that takes a list, a list of old words, and a list of new words; the last two lists should be the same length. It should return a copy of the first argument, but with each word that occurs in the second argument replaced by the corresponding word of the third argument:
Please review my code.
(define (substitute2 lst oldlst newlst) (define (maybe-swap elem oldlst newlst) (cond ((null? oldlst) elem) ((equal? (car oldlst) elem) (car newlst)) (else (maybe-swap elem (cdr oldlst) (cdr newlst))))) (if (list? lst) (map (lambda (elem) (substitute2 elem oldlst newlst)) lst) (if (= (length oldlst) (length newlst)) (maybe-swap lst oldlst newlst) (error "invalid length"))))
How can I make this code better and more efficient? Did I overuse define?