Iterative MAPCAR
authorDavid Vázquez <davazp@gmail.com>
Fri, 8 Feb 2013 22:47:13 +0000 (23:47 +0100)
committerDavid Vázquez <davazp@gmail.com>
Fri, 8 Feb 2013 22:47:13 +0000 (23:47 +0100)
ecmalisp.lisp

index 9951c03..cdf33d0 100644 (file)
     (concat-two s1 s2))
 
   (defun mapcar (func list)
-    (if (null list)
-        '()
-        (cons (funcall func (car list))
-              (mapcar func (cdr list)))))
+    (let* ((head (cons 'sentinel nil))
+          (tail head))
+      (while (not (null list))
+       (let ((new (cons (funcall func (car list)) nil)))
+         (rplacd tail new)
+         (setq tail new
+               list (cdr list))))
+      (cdr head)))
 
   (defun identity (x) x)