previous | index | next

Vector Example 2

Randomly fill a vector with integers from the interval [1,100].
     (define randomly-fill!
       (lambda (v)
	 (let ((size (vector-length v)))
	   (define loop
	     (lambda (loc)
	       (cond ((= loc size))
		     (else (vector-set! v loc (+ 1 (random 100)))
			   (loop (+ loc 1))))))
	   (loop 0))))

     (define v (make-vector 10))

     v ⇒ #(0 0 0 0 0 0 0 0 0 0) 

     (randomly-fill! v)

     v ⇒ #(85 94 83 6 10 31 45 19 41 56)

previous | index | next