From: Kyle R. B. <kyl...@gm...> - 2007-02-10 19:20:45
|
Is there a built-in for finding a list's tail? I've implemented 2 versions, one in jscheme: (define-method (tail (lst Pair)) (let loop ((lst lst)) (cond ((null? lst) #f) ((null? (cdr lst)) lst) (else (loop (cdr lst)))))) and one in java: /** * Finds a list's tail by traversing the list. * @param lst the list to find the tail of * @error ClassCastException if lst is not a proper list. */ public static Pair tail (Pair lst) { Object tmp = rest(lst); while (isPair(tmp)) { lst = (Pair)tmp; tmp = rest(tmp); } return lst; } The java version is quite a bit faster: (define *1k-list* (let loop ((max 1000) (res (list))) (cond ((= 0 max) res) (else (loop (- max 1) (cons max res)))))) (time (dotimes (ii 1000) (my-tail *1k-list*))) $7 = (() (479 msec) (147624 bytes)) (time (dotimes (ii 1000) (U.tail *1k-list*))) $8 = (() (42 msec) (189360 bytes)) Is it ok if I add that method as a static to the utils and add unit tests? Thanks, Kyle |