;; ;; FILE: list-functions.rkt ;; AUTHOR: Eugene Wallingford ;; DATE: 2024/02/12 ;; COMMENT: Recursive functions for processing lists that are defined ;; in class during Session 9. ;; #lang racket (require rackunit) ;; -------------------------------------------------------------------- (define (list-length lon) (if (null? lon) 0 (+ 1 (list-length (rest lon))))) ; can use add1 here ;; -------------------------------------------------------------------- ;; bonus function: so similar to any-bigger-than? ! (define (list-member? n lon) (if (null? lon) #f (or (= n (first lon)) (list-member? n (rest lon))))) ;; -------------------------------------------------------------------- (define (remove-upto s los) (if (null? los) '() (if (eq? (first los) s) los (remove-upto s (rest los))))) (check-equal? (remove-upto 'a '(a b c)) '(a b c)) (check-equal? (remove-upto 'b '(a b c)) '(b c)) (check-equal? (remove-upto 'd '(a b c)) '()) (check-equal? (remove-upto 'a '()) '()) (check-equal? (remove-upto 'a '(b d f g a a a a a a)) '(a a a a a a)) ;; -------------------------------------------------------------------- (define (remove-first s los) (if (null? los) '() (if (eq? (first los) s) (rest los) (cons (first los) (remove-first s (rest los)))))) (check-equal? (remove-first 'a '(a b c)) '(b c)) (check-equal? (remove-first 'b '(a b c)) '(a c)) (check-equal? (remove-first 'd '(a b c)) '(a b c)) (check-equal? (remove-first 'a '()) '()) (check-equal? (remove-first 'a '(a a a a a a a a a a)) '(a a a a a a a a a)) ;; -------------------------------------------------------------------- (define (remove s los) (if (null? los) '() (if (eq? (first los) s) (remove s (rest los)) (cons (first los) (remove s (rest los)))))) (check-equal? (remove 'a '(a b c)) '(b c)) (check-equal? (remove 'a '(a a a a a a a a a a)) '()) ;; --------------------------------------------------------------------