Steve Schafer <pandeng@telepath.com> wrote:
+---------------
| (define (indexof item list)
| (define (internal-indexof item list count)
| (if (or (null? list) (= count 0))
| 0
| (if (eq? item (car list))
| count
| (internal-indexof item (cdr list) (+ count 1)))))
| (internal-indexof item list 1))
+---------------
What's up with that "(= count 0)" term? It doesn't do anything useful,
since it can never be true. Why not just:
(define (indexof item list)
(define (internal-indexof item list count)
(cond
((null? list) #f)
((eq? item (car list)) count)
(else (internal-indexof item (cdr list) (+ count 1)))))
(internal-indexof item list 1))
Note: I've also changed the "not-found" return value from "0" to "#f",
since that's more in line with the rest of Scheme (member, assoc, etc.).
Finally, although the original poster specified a "1-based" result, a
0-based result is more in keeping with a name like "index". I would
encourage him to change the spec of "indexof" to give a 0-based result,
and thus change the last expr above to (internal-indexof item list 0)
so that (indexof 'a '(c b a d e f)) ==> 2.
-Rob
-----
Rob Warnock, 31-2-510 rpw3@sgi.com
Network Engineering http://reality.sgi.com/rpw3/
Silicon Graphics, Inc. Phone: 650-933-1673
1600 Amphitheatre Pkwy. PP-ASEL-IA
Mountain View, CA 94043