Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce iterations in concatenate! #946

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading