X-Git-Url: https://git.hcoop.net/bpt/guile.git/blobdiff_plain/e5d2c2fa4fb0586308f4d716c9ae9d3ce47ae237..8a7fb63c90b94738d9797080409dcb078fa3e8f7:/ice-9/common-list.scm diff --git a/ice-9/common-list.scm b/ice-9/common-list.scm index 02d1858e2..c5c8c0609 100644 --- a/ice-9/common-list.scm +++ b/ice-9/common-list.scm @@ -124,6 +124,14 @@ Analogous to some but returns #t as soon as an application of PRED returns #f, or #f otherwise." (not (apply every pred ls))) +(define-public (count-if pred l) + "Returns the number of elements in L such that (PRED element) +returns true." + (let loop ((n 0) (l l)) + (cond ((null? l) n) + ((pred (car l)) (loop (+ n 1) (cdr l))) + (else (loop n (cdr l)))))) + (define-public (find-if pred l) "Searches for the first element in L such that (PRED element) returns true. If it finds any such element in L, element is @@ -225,10 +233,11 @@ non-#f return values of P." (define-public (uniq l) "Return a list containing elements of L, with duplicates removed." - (if (null? l) - '() - (let ((u (uniq (cdr l)))) - (if (memq (car l) u) - u - (cons (car l) u))))) - + (let loop ((acc '()) + (l l)) + (if (null? l) + (reverse! acc) + (loop (if (memq (car l) acc) + acc + (cons (car l) acc)) + (cdr l)))))