home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / misc / 3611 < prev    next >
Encoding:
Internet Message Format  |  1992-11-12  |  2.4 KB

  1. Path: sparky!uunet!mcsun!uknet!keele!nott-cs!piaggio!anw
  2. From: anw@maths.nott.ac.uk (Dr A. N. Walker)
  3. Newsgroups: comp.lang.misc
  4. Subject: Re: Pointers
  5. Message-ID: <1992Nov12.152032.1576@maths.nott.ac.uk>
  6. Date: 12 Nov 92 15:20:32 GMT
  7. References: <1992Nov7.115620.29967@syacus.acus.oz.au> <1992Nov10.024021.8724@linus.mitre.org> <92Nov10.125426est.47525@neat.cs.toronto.edu> <BxIoDv.72J@mentor.cc.purdue.edu>
  8. Reply-To: anw@maths.nott.ac.uk (Dr A. N. Walker)
  9. Organization: Maths Dept., Nott'm Univ., UK.
  10. Lines: 46
  11.  
  12. In article <BxIoDv.72J@mentor.cc.purdue.edu> hrubin@pop.stat.purdue.edu
  13. (Herman Rubin) writes:
  14. [various uses of pointers to functions, to arrays of pointers, etc.,
  15. deleted]
  16.  
  17.     Indeed these things all have their uses, but they too are all
  18. in Algol, and have been for 24 years now.  Two examples that may [or
  19. may not!] gladden Dr Rubin's heart:
  20.  
  21.   a) a self-seeding PRNG:
  22.  
  23.     PROC REAL myrand := REAL: (myseed; myrand := myprng);
  24.  
  25.      ["myrand" is a pointer to a procedure that delivers a real.  It is
  26.       initialised to point to a procedure that first does the seeding, then
  27.       makes "myrand" point instead to the real PRNG.  The result of following
  28.       the pointer and then obeying the procedure is delivered as the first
  29.       PRN.  Uses of "myrand" after the first go straight to "myprng";  note
  30.       that only one procedure is actually called, as "myrand" itself is a
  31.       pointer, *not* a procedure.]
  32.  
  33.    b) a self-buffering geometric random-number generator:
  34.  
  35.     INT bufsize = 1024;
  36.     [bufsize] REAL buffer;  INT bufp;
  37.     [bufsize] PROC REAL geombuff;
  38.     PROC REAL geom := geombuff[bufsize] :=
  39.        REAL: (fill buffer; geom := geombuff[1]; buffer[bufp := 1]);
  40.     FOR i TO bufsize - 1
  41.        DO geombuff[i] := REAL: (geom := geombuff[bufp +:= 1]; buffer[bufp])
  42.        OD;
  43.     PROC fill buffer = ( ... some code to generate the random numbers
  44.         and fill buffer with them ...);
  45.  
  46.      ["geom" points to one of an array of (anonymous) procedures.  All but
  47.       one of these simply returns an element of "buffer", having updated
  48.       "geom";  the last instead fills the buffer and reinitialises the
  49.       various pointers.  Here there is an implied context switch (but only
  50.       one, except when the buffer is filled) each time "geom" is used, so
  51.       we don't get the full benefits of the buffering unless a heavily-
  52.       optimising compiler recognises that the procedure denotation inside
  53.       the DO-loop can be in-lined.]
  54.  
  55. -- 
  56. Andy Walker, Maths Dept., Nott'm Univ., UK.
  57. anw@maths.nott.ac.uk
  58.