[plt-scheme] Some things about the Vector type that are not mentioned in normal documention

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sat Nov 27 16:30:47 EST 2004

Short answer: use build-vector.

Long answer: what you're missing is either the basics of mutable
objects (in any programming language) or the basics of function calls
(in any (call by value) programming language).

Let me try to explain. When you write this:

  (define bad-v (make-vector 20 (make-vector 10 #f)))

what does the programming language do? Well, it sees that the outer
`make-vector' is a function. So, it evaluates it's arguments. The first
argument is 20, so nothing to do there. The second argument is another
function call. So, it evaluates that one. Same deal: 10 is a value and
so is #f, so the function call itself can proceed. Then, the inner call
to make-vector produces a vector. Just one. Now that single vector is
used to fill in the outer vector, resulting in a vector of size 20 with
the same object in each cell. Put another way, you've only created _2_
vectors. One is the spine and it is filled with 20 _references_ to the
other vector. In C terms, it's like the outer array has 20 pointers to
the same array.

You may want to check out How to Design Programs' part IV
(http://www.htdp.org/).

Robby

At Sat, 27 Nov 2004 13:19:42 -0800, Devlin Bentley wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> As a new user to Scheme, I recently tried to implement a 2D array as a
> vector of vectors in PltScheme.  Obviously things did not go well for
> me (I used Make-Vector and I filled the vector with another set of
> vectors.)
> 
> Now nowheres in the included documentation, t-y-scheme or the PLT
> MzScheme language manual, is anything mentioned about this being an,
> ...., issue.  Actually I still have no idea exactly what happened,
> something to do with graphs (they are mentioned once or twice in small
> print in connection with Vectors) but still, there was definitely an
> issue.
> 
> The thing is, the most straight forward way to create a 2D array (well
> aside from the rather excellent SRFI 25 which has insanely nice
> notation) for a student who has just been told to "use vectors like
> arrays", is to make a vector of vectors.  From the students point of
> view (looking at a vector as being a renamed array, which is not so
> far off after they have taken linear algebra courses), this is
> logically valid, and trying to debug what appears to be logically
> valid code is rather hard!
> 
> The short of it is, it would be nice if something was mentioned in at
> least one of the manuals telling the poor user "Not To Do That".
> 
> (I'd ask what the heck happened, but I am sure that will be covered in
> my 2nd quarter of functional programming, but if anyone wants to hit
> me with a clueX4 so I know what to expect when it does come up.... )
> -- 
> --------
> Devlin Bentley
> Com2Kid at gmail.com


Posted on the users mailing list.