remove local length? utility

svn: r9420
This commit is contained in:
Eli Barzilay 2008-04-23 11:48:06 +00:00
parent e2c4500e41
commit 5d4256921c

View File

@ -109,18 +109,9 @@
;; ls
;; (append l (car ls) (loop (cdr ls))))))]))
;; utility: returns the length for a proper list, #f otherwise; does not handle
;; circular lists
(define (length? x)
(let loop ([x x] [n 0])
(if (pair? x)
(loop (cdr x) (add1 n))
(and (null? x) n))))
(define (remove-duplicates l [=? equal?])
(let ([len (length? l)])
(unless len (raise-type-error 'remove-duplicates "list" l))
(let ([h (cond [(< len 40) #f]
(unless (list? l) (raise-type-error 'remove-duplicates "list" l))
(let ([h (cond [(< (length l) 40) #f]
[(eq? =? eq?) (make-hasheq)]
[(eq? =? equal?) (make-hash)]
[else #f])])
@ -149,4 +140,4 @@
[(eq? =? eq?) (loop memq)]
[(eq? =? eqv?) (loop memv)]
[else (loop (lambda (x seen)
(ormap (lambda (y) (=? x y)) seen)))]))))))
(ormap (lambda (y) (=? x y)) seen)))])))))