Project:
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 ``` ```; maximum sum subsequence (define (max-sum-subseq-1 xv) (let ((n (vector-length xv)) (max-so-far 0)) (do ((i 0 (+ i 1))) ((= i n) max-so-far) (do ((j i (+ j 1))) ((= j n)) (let ((sum 0)) (do ((k i (+ k 1))) ((< j k)) (set! sum (+ sum (vector-ref xv k)))) (set! max-so-far (max max-so-far sum))))))) (define (max-sum-subseq-2a xv) (let ((n (vector-length xv)) (max-so-far 0)) (do ((i 0 (+ i 1))) ((= i n) max-so-far) (let ((sum 0)) (do ((j i (+ j 1))) ((= j n)) (set! sum (+ sum (vector-ref xv j))) (set! max-so-far (max max-so-far sum))))))) (define (max-sum-subseq-2b xv) (define (cumarr x) (+ x 1)) (let* ((n (vector-length xv)) (real-array (make-vector (+ n 1) 0))) (vector-set! real-array (cumarr -1) 0) (do ((i 0 (+ i 1))) ((= i n)) (vector-set! real-array (cumarr i) (+ (vector-ref real-array (cumarr (- i 1))) (vector-ref xv i)))) (let ((max-so-far 0)) (do ((i 0 (+ i 1))) ((= i n) max-so-far) (do ((j i (+ j 1))) ((= j n)) (let ((sum (- (vector-ref real-array (cumarr j)) (vector-ref real-array (cumarr i))))) (set! max-so-far (max max-so-far sum)))))))) (define (max-sum-subseq-3 xv) (let-syntax ((x (syntax-rules () ((x i) (vector-ref xv i))))) (let maxsum3 ((l 0) (u (- (vector-length xv) 1))) (cond ((> l u) 0) ((= l u) (max 0 (x l))) (else (let ((m (quotient (+ l u) 2))) (let ((lmax 0) (sum 0)) (do ((i m (- i 1))) ((< i l)) (set! sum (+ sum (x i))) (set! lmax (max lmax sum))) (let ((rmax 0) (sum 0)) (do ((i (+ m 1) (+ i 1))) ((< u i)) (set! sum (+ sum (x i))) (set! rmax (max rmax sum))) (max (+ lmax rmax) (maxsum3 l m) (maxsum3 (+ m 1) u)))))))))) (define (max-sum-subseq-4 xv) (let ((n (vector-length xv)) (max-so-far 0) (max-ending-here 0)) (do ((i 0 (+ i 1))) ((= i n) max-so-far) (set! max-ending-here (max (+ max-ending-here (vector-ref xv i)) 0)) (set! max-so-far (max max-so-far max-ending-here))))) (define (max-sum-subsequence xs) (let loop ((xs xs) (max-ending-here 0) (max-so-far 0)) (if (null? xs) max-so-far (let ((max-ending-here (max (+ max-ending-here (car xs)) 0))) (loop (cdr xs) max-ending-here (max max-ending-here max-so-far)))))) (define xs '(31 -41 59 26 -53 58 97 -93 -23 84)) (define xv #(31 -41 59 26 -53 58 97 -93 -23 84)) (display (max-sum-subseq-1 xv)) (newline) (display (max-sum-subseq-2a xv)) (newline) (display (max-sum-subseq-2b xv)) (newline) (display (max-sum-subseq-3 xv)) (newline) (display (max-sum-subseq-4 xv)) (newline) (display (max-sum-subsequence xs)) (newline) ```
 ```1 2 3 4 5 6 ``` ```187 187 187 187 187 187 ```