1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-23 13:00:34 +02:00

(map!): Change to a tail-recursive form.

This commit is contained in:
Kevin Ryde 2004-12-05 22:26:29 +00:00
parent 9fa5fdc6cd
commit 49046d53e0

View file

@ -683,20 +683,21 @@
(define (map! f list1 . rest) (define (map! f list1 . rest)
(if (null? rest) (if (null? rest)
(let lp ((l list1)) (let lp ((l list1))
(if (null? l) (if (null? l)
'() list1
(begin (begin
(set-car! l (f (car l))) (set-car! l (f (car l)))
(set-cdr! l (lp (cdr l))) (lp (cdr l)))))
l))) (let ((res (cons 123 list1)))
(let lp ((l (cons list1 rest)) (res list1)) (let lp ((l (cons list1 rest)) (endcell res))
(if (any1 null? l) (if (any1 null? l)
'() (begin
(begin (set-cdr! endcell '()) ;; in case list1 was not the shortest
(set-car! res (apply f (map1 car l))) (cdr res))
(set-cdr! res (lp (map1 cdr l) (cdr res))) (begin
res))))) (set-car! (car l) (apply f (map1 car l)))
(lp (map1 cdr l) (car l))))))))
(define (pair-for-each f clist1 . rest) (define (pair-for-each f clist1 . rest)
(if (null? rest) (if (null? rest)