;; ------------------------------------------------------------------------ ;; | FILE : list-index.rkt | ;; | AUTHOR : Eugene Wallingford | ;; | CREATION DATE : 2023/03/28 | ;; | DESCRIPTION : list-index returns the 0-based position of an | ;; | item in a list, or -1 if not found. We last | ;; | saw this function in Session 17 when learning | ;; | about letrec. | ;; ------------------------------------------------------------------------ #lang racket (provide list-index) (define list-index (lambda (target los) (letrec ((list-index-helper (lambda (los pos) (cond ((null? los) -1) ((eq? target (first los)) pos) (else (list-index-helper (rest los) (add1 pos))))))) (list-index-helper los 0)))) ;; ----- END OF FILE -------------------------------------------------------