[ create a new paste ] login | about

Link: http://codepad.org/tCoLS3CO    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Nov 14:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(define (puddle land)
  (let loop ((volume 0) (lmax 0) (rmax 0) (left 0)
             (right (- (vector-length land) 1)))
    (cond ((<= right left) volume)
          ((< lmax (vector-ref land left))
            (loop volume (vector-ref land left) rmax left right))
          ((< rmax (vector-ref land right))
            (loop volume lmax (vector-ref land right) left right))
          ((< rmax lmax)
            (loop (+ volume rmax (- (vector-ref land right)))
                  lmax rmax left (- right 1)))
          (else (loop (+ volume lmax (- (vector-ref land left)))
                      lmax rmax (+ left 1) right))))) 

(display (puddle '#(2 5 1 2 3 4 7 7 6))) (newline)
(display (puddle '#(2 5 1 3 1 2 1 7 7 6))) (newline)
(display (puddle '#(2 7 2 7 4 7 1 7 3 7))) (newline)
(display (puddle '#(6 7 7 4 3 2 1 5 2))) (newline)


Output:
1
2
3
4
10
17
18
10


Create a new paste based on this one


Comments: