;; ----------------------------------------------------------------------- ;; | | ;; | FILE : positions-of.rkt | ;; | AUTHOR : Eugene Wallingford | ;; | CREATION DATE : 2020/02/18 | ;; | | ;; | DESCRIPTION : Solution to Problem 5 on Homework 4. Notice | ;; | that the helper function positions-of-at | ;; | is tail recursive! | ;; | | ;; | REQUIRES : Uses Rackunit for testing. | ;; | | ;; ----------------------------------------------------------------------- #lang racket (require rackunit) ; ------------------------------------------------------------------------- ; positions-of (interface procedure) ; ------------------------------------------------------------------------- (define positions-of (lambda (s los) (positions-of-at s los 0))) (define positions-of-at (lambda (s los position) (if (null? los) '() (if (eq? s (first los)) (cons position (positions-of-at s (rest los) (add1 position))) (positions-of-at s (rest los) (add1 position)))))) (check-equal? (positions-of 'a '(a b a c d a e f g a h i j k)) '(0 2 5 9)) (check-equal? (positions-of 'a '(b c d e f g h i j k)) '()) ; -------------------------------------------------------------------------