;; Some examples showing innacuracies using floating-point numbers, ;; and how to try to get around them. ;; ; make-list : natnum any -> list-of-any ; returns a list of n copies of s (define (make-list n s) (cond [(zero? n) empty] [else (cons s (make-list (sub1 n) s))])) ; example lists for addition ; list1: 1.0e16 followed by 101 5's ; list2: 101 5's followed by 1.0e16 ; list3: 50 5's followed by 1.0e16 followed by 51 5's (define list1 (cons #i1.0e16 (make-list 101 5))) (define list2 (append (make-list 101 5) (list #i1.0e16))) (define list3 (append (make-list 50 5) (cons #i1.0e16 (make-list 51 5)))) (define janus (list 99 #i3.2e+270 -99 #i-2.4e+270 1234 4 #i-8e+269 -4)) ; sum-r : list-of-number -> number ; Returns the right-associative sum of the elements of the list. (define (sum-r l) (foldr + 0 l)) ; sum-l : list-of-number -> number ; Returns the left-associative sum of the elements of the list. (define (sum-l l) (foldl + 0 l)) ; order : list-of-number -> list-of-number ; Builds a list of the given numbers, ordered in increasing absolute value. ; (Note: "quicksort" is a built-in DrScheme function!) (define (order l) (quicksort l (lambda (x y) (> (abs x) (abs y)))))