# Added reverse-from-end functions.

Pascal J. Bourguignon [2017-04-06 23:22]
```Added reverse-from-end functions.
```
Filename
p05.lisp
```diff --git a/p05.lisp b/p05.lisp
index 8fbc532..61be358 100644
--- a/p05.lisp
+++ b/p05.lisp
@@ -47,22 +47,51 @@ P05 (*) Reverse a list.

+;; Other Common Lisp solutions:

-;; (mapcar (lambda (fun) (mapcar fun '(() (a) (a b) (a b c))))
-;;         (list (function iterative-reverse-do)
-;;               (function iterative-reverse-loop)
-;;               (function naive-recursive-reverse)
-;;               (function recursive-reverse)
-;;               (function my-reverse)))
-;; --> ((NIL (A) (B A) (C B A))
-;;      (NIL (A) (B A) (C B A))
-;;      (NIL (A) (B A) (C B A))
-;;      (NIL (A) (B A) (C B A))
-;;      (NIL (A) (B A) (C B A)))
+(defun reduce-reverse-from-start (list)
+  (reduce (lambda (d a) (cons a d)) list :initial-value '()))

+(defun reduce-reverse-from-end (list)
+  (reduce (lambda (a d) (append d (list a))) list :initial-value '()  :from-end t))

+(defun reduce-reverse-from-end-with-pointer (list)
+  (reduce (let ((pointer nil)
+            (lambda (a d)
+              (if pointer
+                  (setf (cdr pointer) (list a)
+                        pointer (cdr pointer))
+                  (setf head (list a)
+          list
+          :initial-value '()
+          :from-end t))

+(defun revappend-reverse (list)
+  (revappend list nil))
+
+(defun nreconc-reverse (list)
+  (nreconc (copy-list list) nil))
+
+
+
+(mapc (lambda (fun)
+          (assert (equal (mapcar fun '(() (a) (a b) (a b c) (a b c d) (a b c d e)))
+                         '(nil (a) (b a) (c b a) (d c b a) (e d c b a)))))
+      (list (function iterative-reverse-do)
+            (function iterative-reverse-loop)
+            (function naive-recursive-reverse)
+            (function recursive-reverse)
+            (function my-reverse)
+            (function reduce-reverse-from-start)
+            (function reduce-reverse-from-end)
+            (function reduce-reverse-from-end-with-pointer)
+            (function revappend-reverse)
+            (function nreconc-reverse)))
+

;; A tail-recursive solution for the reversing of the list in place.
@@ -86,4 +115,13 @@ P05 (*) Reverse a list.
;;     (4 3 2 1)

+
+(defun nreconc-nreverse (list)
+  (nreconc list nil))
+
+
+
+(defun reduce-append (l1 l2)
+  (reduce (function cons) l1 :initial-value l2 :from-end t))
+
;;;; THE END ;;;;```
ViewGit