;;; ******************************************************************** ;;; util.ss ;;; Susan Fox ;;; December 27, 2000 ;;; Contains basic utility operations (define UTIL.SSLOADED #t) ;;; ********************************************************************* ;;; warn takes a string and prints a warning to the screen ;;; (define warn (lambda (str) (printf "Warning: ~s~n" str))) ;;; outliers takes two lists, and returns a list containing all ;;; those elements from the first list that are not in the second ;;; list ;;; (define outliers (lambda (lst1 lst2) (cond [(null? lst1) '()] [(member (car lst1) lst2) (outliers (cdr lst1) lst2)] [else (cons (car lst1) (outliers (cdr lst1) lst2))]))) ;;; list-at-i takes a position, i and a list and returns the ;;; list starting at that position. position zero returns the ;;; whole list, position 1 returns the cdr of the list, etc. ;;; (define list-at-i (lambda (i lst) (if (null? lst) (error 'list-at-i "Index value out of range") (if (zero? i) lst (list-at-i (sub1 i) (cdr lst)))))) ;;; int-average takes some number of numbers and computes the ;;; average of them, returning it as an integer value by ;;; using inexact->exact and truncate ;;; (define int-average (lambda nums (if (and (= (length nums) 1) (list? (car nums))) (apply int-average (car nums)) (if (null? nums) (error 'int-average "Must pass arguments to int-average") (round (inexact->exact (/ (apply + nums) (length nums)))))))) ;;; average returns a real or rational average of its arguments ;;; (define average (lambda nums (if (and (= (length nums) 1) (list? (car nums))) (apply average (car nums)) (if (null? nums) (error 'average "Must pass arguments to int-average") (/ (apply + nums) (length nums))))))