;;; ;;; CS 200 ;;; ;;; listprocs.ss ;;; ;;; This file contains many of the list procedures we have defined in class. ;;; (require-library "trace.ss") (define (insertlg f lst start) (if (null? lst) start (f (car lst) (insertlg f (cdr lst) start)))) ;;; Evaluates to the list parameter with exactly one instance of el removed. (define (delete lst el) (if (null? lst) null ;; okay to not find it now: (error "Element not found!") (if (eqv? (car lst) el) (cdr lst) (cons (car lst) (delete (cdr lst) el))))) (define (find-most cf lst) (insertlg (lambda (c1 c2) (if (cf c1 c2) c1 c2)) lst (car lst))) (define (revintsto n) (if (= n 0) null (cons n (revintsto (- n 1))))) (define (intsto n) (reverse (revintsto n))) (define (rand-int-list n) (if (= n 0) null (cons (random 100) (rand-int-list (- n 1))))) (define (filter f lst) (insertlg (lambda (el rest) (if (f el) (cons el rest) rest)) lst null)) ;;; ;;; member? evaluates to true if thing is in list (could use memq for this) ;;; (define (member? thing lst) (if (null? lst) #f (if (eq? thing (car lst)) #t (member? thing (cdr lst)))))