mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-12 06:41:13 +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'.
|
||||
;;
|
||||
(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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue