Replace
Table of Contents

This function is derived from the Common Lisp Replace function.

Code

(defun replace (sequence-1 sequence-2 start1 end1 start2 end2 / sequence-N n)
  (if (not start1) (setq start1 0))
  (if (not start2) (setq start2 0))
  (if (not end1) (setq end1 (length sequence-1)))
  (if (not end2) (setq end2 (if (listp sequence-2) (length sequence-2) 0)))
  (setq n 0)
  (while (< n start1 (length sequence-1))
    (setq sequence-N (cons (nth n sequence-1) sequence-N)
          n (1+ n)
    )
  )
  (if (listp sequence-2)
    (if sequence-2
      (while (<= start2 end2 (1- (length sequence-2)))
        (setq sequence-N (cons (nth start2 sequence-2) sequence-N)
              start2     (1+ start2)
        )
      )
    )
    (setq sequence-N (cons sequence-2 sequence-N))
  )
  (while (< (setq end1 (1+ end1)) (length sequence-1))
    (setq sequence-N (cons (nth end1 sequence-1) sequence-N))
  )
  (reverse sequence-N)
)