;; ;; 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 (lambda (lon) (if (null? lon) 0 (+ 1 (list-length (rest lon)))))) ; can use add1 here ;; -------------------------------------------------------------------- (define list-member? ; bonus function: so similar to any-bigger-than? ! (lambda (n lon) (if (null? lon) #f (or (= n (first lon)) (list-member? n (rest lon)))))) ;; -------------------------------------------------------------------- (define remove-upto (lambda (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 (lambda (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 (lambda (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)) '()) ;; --------------------------------------------------------------------