mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-13 15:10:34 +02:00
(map1): Rewrite to be tail-recursive.
Thanks to Panagiotis Vossos for the bug report.
This commit is contained in:
parent
cdd2e6500e
commit
513a3bd72d
1 changed files with 20 additions and 15 deletions
|
@ -603,10 +603,15 @@
|
||||||
;; Internal helper procedure. Map `f' over the single list `ls'.
|
;; Internal helper procedure. Map `f' over the single list `ls'.
|
||||||
;;
|
;;
|
||||||
(define (map1 f ls)
|
(define (map1 f ls)
|
||||||
(let lp ((l ls))
|
(if (null? ls)
|
||||||
(if (null? l)
|
ls
|
||||||
'()
|
(let ((ret (list (f (car ls)))))
|
||||||
(cons (f (car l)) (lp (cdr l))))))
|
(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
|
;; This `map' is extended from the standard `map'. It allows argument
|
||||||
;; lists of different length, so that the shortest list determines the
|
;; lists of different length, so that the shortest list determines the
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue