summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Shinn <alexshinn@gmail.com>2024-01-09 09:42:27 +0900
committerGitHub <noreply@github.com>2024-01-09 09:42:27 +0900
commitcc6a3d10e5a5ea5bd643395f3e2cc3ebbd822dcf (patch)
tree1d1bd1250ce0d4c3f3ddc0139fd959f34404776e
parentaf41e2b01d86f40f2346bb175f2abe2bf85b1946 (diff)
parent70989e0cef3ccb83d7391582c1d0ab0459bb1954 (diff)
Merge pull request #945 from ekaitz-zarraga/concatenate!
Fix #944: concatenate! work with empty lists in any position
-rw-r--r--lib/srfi/1/misc.scm13
-rw-r--r--lib/srfi/1/test.sld4
2 files changed, 11 insertions, 6 deletions
diff --git a/lib/srfi/1/misc.scm b/lib/srfi/1/misc.scm
index ce360dc8..843ed19d 100644
--- a/lib/srfi/1/misc.scm
+++ b/lib/srfi/1/misc.scm
@@ -15,12 +15,13 @@
(define (concatenate! lists)
(if (null? lists)
'()
- (let lp ((ls lists))
- (cond ((not (pair? (cdr ls)))
- (car lists))
- (else
- (set-cdr! (last-pair (car ls)) (cadr ls))
- (lp (cdr ls)))))))
+ (fold (lambda (el acc)
+ (cond
+ ((null? acc) el)
+ ((null? el) acc)
+ (else (begin (set-cdr! (last-pair acc) el) acc))))
+ (car lists)
+ (cdr lists))))
(define (append-reverse rev tail)
(if (null? rev) tail (append-reverse (cdr rev) (cons (car rev) tail))))
diff --git a/lib/srfi/1/test.sld b/lib/srfi/1/test.sld
index b280b12b..94910f88 100644
--- a/lib/srfi/1/test.sld
+++ b/lib/srfi/1/test.sld
@@ -77,6 +77,10 @@
(test 'a (append '() 'a))
(test '(x y) (append '(x y)))
(test '() (append))
+ (test (list 'a) (append! '() (list 'a)))
+ (test (list 'a 'b) (append! (list 'a) '() '() (list 'b)))
+ (test (list 'x 'y) (append! (list 'x 'y)))
+ (test '() (append!))
(test '(c b a) (reverse '(a b c)))
(test '((e (f)) d (b c) a) (reverse '(a (b c) d (e (f)))))
(test '((one 1 odd) (two 2 even) (three 3 odd)) (zip '(one two three) '(1 2 3) '(odd even odd even odd even odd even)))