+ ((listp seq)
+ (if b
+ (let ((diff (- b a)))
+ (cond
+ ((zerop diff) ())
+ ((minusp diff)
+ (error "Start index must be smaller than end index"))
+ (t
+ (let* ((drop-a (copy-list (nthcdr a seq)))
+ (pointer drop-a))
+ (dotimes (_ (1- diff))
+ (setq pointer (cdr pointer))
+ (when (null pointer)
+ (error "Ending index larger than length of list")))
+ (rplacd pointer ())
+ drop-a))))
+ (copy-list (nthcdr a seq))))
+ ((vectorp seq)
+ (let* ((b (or b (length seq)))
+ (size (- b a))
+ (new (make-array size :element-type (array-element-type seq))))
+ (do ((i 0 (1+ i))
+ (j a (1+ j)))
+ ((= j b) new)
+ (aset new i (aref seq j)))))
+ (t (not-seq-error seq))))
+
+(defun copy-seq (sequence)
+ (subseq sequence 0))
+
+
+;;; Reduce (based on SBCL's version)
+
+(defun reduce (function sequence &key key from-end (start 0) end (initial-value nil ivp))
+ (let ((key (or key #'identity))
+ (end (or end (length sequence))))
+ (if (= end start)
+ (if ivp initial-value (funcall function))
+ (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
+ `(let ((sequence
+ ,(if from-end
+ `(reverse (nthcdr ,start ,sequence))
+ `(nthcdr ,start ,sequence))))
+ (do ((count (if ,ivp ,start (1+ ,start))
+ (1+ count))
+ (sequence (if ,ivp sequence (cdr sequence))
+ (cdr sequence))
+ (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
+ ,(if from-end
+ `(funcall ,function (funcall ,key (car sequence)) value)
+ `(funcall ,function value (funcall ,key (car sequence))))))
+ ((>= count ,end) value)))))
+ (if from-end
+ (reduce-list function sequence key start end initial-value ivp t)
+ (reduce-list function sequence key start end initial-value ivp nil))))))