home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.scheme
- Path: sparky!uunet!mcsun!news.funet.fi!tut!pk
- From: pk@cs.tut.fi (Kellom{ki Pertti)
- Subject: Initializing vector elements
- Message-ID: <PK.92Aug19100516@talitiainen.cs.tut.fi>
- Sender: usenet@funet.fi (#Kotilo NEWS system )
- Nntp-Posting-Host: talitiainen.cs.tut.fi
- Organization: Tampere Univ. of Technology, Finland.
- Distribution: comp
- Date: Wed, 19 Aug 1992 08:05:16 GMT
- Lines: 51
-
- Yesterday, a friend of mine was bitten by the semantics of
- make-vector. He wanted to make a matrix by creating a vector of
- vectors. So he goes on to write
-
- (define m (make-vector 10 (make-vector 10 0)))
-
- and gets what he expects:
-
- #(#(0 0 0 0 0 0 0 0 0 0)
- #(0 0 0 0 0 0 0 0 0 0)
- ...
- #(0 0 0 0 0 0 0 0 0 0))
-
- Now he changes the element at 0,0 to 42, and is surprised because the
- entire leftmost column has changed. Being a teacher, I have seen this
- happen countless times before.
-
- It is clear what has happened: all the rows of the matrix are really
- the same row. What bothers me is that there really is no clean way of
- building a matrix like the one intended. I can think of two ways:
- explicitely looping over the rows, or using list->vector. The first
- one is clumsy at best, and the second one feels like a kludge to me
-
- (let ((m (make-vector 10)))
- (do ((i 0 (+ i 1)))
- ((= i 10) m)
- (vector-set! m i (make-vector 10 0))))
-
- (list->vector (map (lambda (n) (make-vector n 0))
- '(10 10 10 10 10 10 10 10 10 10)))
-
- Has there been discussion about what the second argument of
- make-vector should be? I would like to see make-vector to take a
- procedure that would be called for every element of the vector. Thus
- the example would be written as
-
- (define m (make-vector 10 (lambda () (make-vector 10 0))))
-
- This seems like a Scheme way of doing things. I know it is possibly
- too late to change it, but I thought I'd bring it up just for the sake
- of discussion.
-
- If anyone can prove me wrong by giving an elegant way of defining m
- within R4RS Scheme, I'll be delighted.
- --
- Pertti Kellom\"aki (TeX format) # These opinions are mine,
- Tampere Univ. of TeXnology # ALL MINE !
- Software Systems Lab # (but go ahead and use them, if you like)
-
-
- --
-