Skip to content

Commit

Permalink
Merge pull request #946 from ekaitz-zarraga/faster-concatenate!
Browse files Browse the repository at this point in the history
Reduce iterations in concatenate!
  • Loading branch information
ashinn authored Jan 10, 2024
2 parents a67e759 + 967b888 commit 97a04bd
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/srfi/1/misc.scm
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
(define (concatenate! lists)
(if (null? lists)
'()
(fold (lambda (el acc)
(cond
((null? acc) el)
((null? el) acc)
(else (begin (set-cdr! (last-pair acc) el) acc))))
(car lists)
(cdr lists))))
(let loop ((acc '())
(prev '())
(rem lists))
(cond
((null? rem) acc)
((null? acc) (let ((cur (car rem))) (loop cur cur (cdr rem))))
((null? (car rem)) (loop acc prev (cdr rem)))
(else (let ((cur (car rem)))
(set-cdr! (last-pair prev) cur)
(loop acc cur (cdr rem))))))))

(define (append-reverse rev tail)
(if (null? rev) tail (append-reverse (cdr rev) (cons (car rev) tail))))
Expand Down

0 comments on commit 97a04bd

Please sign in to comment.