1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-12 14:50:19 +02:00

(map1): Rewrite to be tail-recursive.

Thanks to Panagiotis Vossos for the bug report.
This commit is contained in:
Thien-Thi Nguyen 2002-01-21 01:11:35 +00:00
parent cdd2e6500e
commit 513a3bd72d

View file

@ -603,10 +603,15 @@
;; Internal helper procedure. Map `f' over the single list `ls'.
;;
(define (map1 f ls)
(let lp ((l ls))
(if (null? l)
'()
(cons (f (car l)) (lp (cdr l))))))
(if (null? ls)
ls
(let ((ret (list (f (car ls)))))
(let lp ((ls (cdr ls)) (p ret)) ; tail pointer
(if (null? ls)
ret
(begin
(set-cdr! p (list (f (car ls))))
(lp (cdr ls) (cdr p))))))))
;; This `map' is extended from the standard `map'. It allows argument
;; lists of different length, so that the shortest list determines the