home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / scheme / 2049 < prev    next >
Encoding:
Text File  |  1992-08-18  |  2.2 KB  |  64 lines

  1. Newsgroups: comp.lang.scheme
  2. Path: sparky!uunet!mcsun!news.funet.fi!tut!pk
  3. From: pk@cs.tut.fi (Kellom{ki Pertti)
  4. Subject: Initializing vector elements
  5. Message-ID: <PK.92Aug19100516@talitiainen.cs.tut.fi>
  6. Sender: usenet@funet.fi (#Kotilo NEWS system )
  7. Nntp-Posting-Host: talitiainen.cs.tut.fi
  8. Organization: Tampere Univ. of Technology, Finland.
  9. Distribution: comp
  10. Date: Wed, 19 Aug 1992 08:05:16 GMT
  11. Lines: 51
  12.  
  13. Yesterday, a friend of mine was bitten by the semantics of
  14. make-vector. He wanted to make a matrix by creating a vector of
  15. vectors. So he goes on to write
  16.  
  17.   (define m (make-vector 10 (make-vector 10 0)))
  18.  
  19. and gets what he expects:
  20.  
  21.  #(#(0 0 0 0 0 0 0 0 0 0) 
  22.    #(0 0 0 0 0 0 0 0 0 0) 
  23.    ...
  24.    #(0 0 0 0 0 0 0 0 0 0))
  25.  
  26. Now he changes the element at 0,0 to 42, and is surprised because the
  27. entire leftmost column has changed. Being a teacher, I have seen this
  28. happen countless times before.
  29.  
  30. It is clear what has happened: all the rows of the matrix are really
  31. the same row. What bothers me is that there really is no clean way of
  32. building a matrix like the one intended.  I can think of two ways:
  33. explicitely looping over the rows, or using list->vector. The first
  34. one is clumsy at best, and the second one feels like a kludge to me
  35.  
  36. (let ((m (make-vector 10)))
  37.     (do ((i 0 (+ i 1)))
  38.     ((= i 10) m)
  39.       (vector-set! m i (make-vector 10 0))))
  40.  
  41. (list->vector (map (lambda (n) (make-vector n 0)) 
  42.                    '(10 10 10 10 10 10 10 10 10 10)))
  43.  
  44. Has there been discussion about what the second argument of
  45. make-vector should be? I would like to see make-vector to take a
  46. procedure that would be called for every element of the vector. Thus
  47. the example would be written as
  48.  
  49.   (define m (make-vector 10 (lambda () (make-vector 10 0))))
  50.  
  51. This seems like a Scheme way of doing things. I know it is possibly
  52. too late to change it, but I thought I'd bring it up just for the sake
  53. of discussion.
  54.  
  55. If anyone can prove me wrong by giving an elegant way of defining m
  56. within R4RS Scheme, I'll be delighted.
  57. --
  58. Pertti Kellom\"aki (TeX format)  #       These opinions are mine, 
  59.   Tampere Univ. of TeXnology     #              ALL MINE !
  60.       Software Systems Lab       #  (but go ahead and use them, if you like)
  61.  
  62.  
  63. -- 
  64.