fork download
  1. ; sorting without duplicates
  2.  
  3. (define (partition lt? xs) ; assumes sorted xs
  4. (let loop ((xs xs) (prev #f) (ys (list)) (zs (list)))
  5. (cond ((null? xs) (values (reverse ys) (reverse zs)))
  6. ((equal? (car xs) prev)
  7. (loop (cdr xs) prev ys (cons (car xs) zs)))
  8. (else (loop (cdr xs) (car xs) (cons (car xs) ys) zs)))))
  9.  
  10. (define (sort-dups lt? xs)
  11. (let loop ((xs xs) (zs (list)))
  12. (if (null? xs)
  13. (apply append (reverse zs))
  14. (call-with-values
  15. (lambda () (partition lt? xs))
  16. (lambda (first rest)
  17. (loop rest (cons first zs)))))))
  18.  
  19. (display (sort-dups < (sort '(2 9 1 5 1 4 9 7 2 1 4) <))) (newline)
  20. (display (sort-dups < (sort '(1 1 1 1 1 1 1 1 1) <))) (newline)
  21. (display (sort-dups < (sort '() <))) (newline)
  22. (display (sort-dups < (sort '(7 3 1 6 2 5 4) <))) (newline)
Success #stdin #stdout 0.01s 8672KB
stdin
Standard input is empty
stdout
(1 2 4 5 7 9 1 2 4 9 1)
(1 1 1 1 1 1 1 1 1)
()
(1 2 3 4 5 6 7)