implements delete-duplicates(!) tail-recursively

This commit is contained in:
stibear 2014-06-26 22:44:38 +09:00
parent b77fac6ce1
commit 2526474fb3
1 changed files with 325 additions and 325 deletions

View File

@ -550,23 +550,23 @@
(define (delete-duplicates list . =) (define (delete-duplicates list . =)
(let ((= (if (null? =) equal? (car =)))) (let ((= (if (null? =) equal? (car =))))
(let rec ((list list)) (let rec ((list list) (cont values))
(if (null? list) (if (null? list)
list (cont '())
(let* ((x (car list)) (let* ((x (car list))
(rest (cdr list)) (rest (cdr list))
(deleted (rec (delete x list =)))) (deleted (delete x rest =)))
(if (eq? rest deleted) list (cons x deleted))))))) (rec deleted (lambda (y) (cont (cons x y)))))))))
(define (delete-duplicates! list . =) (define (delete-duplicates! list . =)
(let ((= (if (null? =) equal? (car =)))) (let ((= (if (null? =) equal? (car =))))
(let rec ((list list)) (let rec ((list list) (cont values))
(if (null? list) (if (null? list)
list (cont '())
(let* ((x (car list)) (let* ((x (car list))
(rest (cdr list)) (rest (cdr list))
(deleted (rec (delete! x list =)))) (deleted (delete! x list =)))
(if (eq? rest deleted) list (cons x deleted))))))) (rec deleted (lambda (y) (cont (cons x y)))))))))
(export delete delete-duplicates (export delete delete-duplicates
delete! delete-duplicates!) delete! delete-duplicates!)