Skip to content

Commit

Permalink
Merge pull request #945 from ekaitz-zarraga/concatenate!
Browse files Browse the repository at this point in the history
Fix #944: concatenate! work with empty lists in any position
  • Loading branch information
ashinn authored Jan 9, 2024
2 parents af41e2b + 70989e0 commit cc6a3d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/srfi/1/misc.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down
4 changes: 4 additions & 0 deletions lib/srfi/1/test.sld
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down

0 comments on commit cc6a3d1

Please sign in to comment.