;; 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) (cond [(empty? l) 0] [(cons? l) (+ (first l) (sum-r (rest l)))])) ; sum-l : list-of-number -> number ; Returns the left-associative sum of the elements of the list. (define (sum-l l) (sum-r (reverse l))) ; order : list-of-number -> list-of-number ; Builds a list of the given numbers, ordered in increasing absolute value. (define (order l) (cond [(empty? l) empty] [(cons? l) (insert (first l) (order (rest l)))])) (define (insert n l) (cond [(empty? l) (cons n empty)] [(cons? l) (cond [(< (abs n) (abs (first l))) (cons n l)] [else (cons (first l) (insert n (rest l)))])]))