[plt-scheme] Re: plt-scheme digest, Vol 1 #1089 - 7 msgs

From: David G.Kay (kay at uci.edu)
Date: Sun Nov 28 09:01:44 EST 2004

I'll bite:  Why is the second solution much
faster than the first one?
								--DGK

> As for your problem, you may wish to consider two different solutions:
>
>   ;; Nat Nat (Nat Nat -> X) -> Matrix[X]
>   (define (build-matrix n m f)
>     (build-vector n (lambda (i) (build-vector m (lambda (j) (f i 
> j))))))
>
>   ;; Matrix[X] Nat Nat -> X
>   (define (matrix-ref M i j)
>      (vector-ref (vector-ref M i) j)))
>   ;; suitable dimension check omitted
>
> or
>
>   ;; Nat Nat (Nat Nat -> X) -> Matrix[X]
>   (define (build-matrix n m f)
>     (list->vector (apply append (build-list n (lambda (i) (build-list m
> (lambda (j) (f i j))))))))
>
> with a suitable deferencing schema. It's much faster than the first 
> one.
>
> -- Matthias



Posted on the users mailing list.