home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / pop / 231 < prev    next >
Encoding:
Internet Message Format  |  1993-01-29  |  3.2 KB

  1. From: sfk@otter.hpl.hp.com (Steve Knight)
  2. Date: Thu, 28 Jan 1993 15:44:58 GMT
  3. Subject: Re: Re: dlocals etc. (and shallow binding)
  4. Message-ID: <116670052@otter.hpl.hp.com>
  5. Organization: Hewlett-Packard Laboratories, Bristol, UK.
  6. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!paladin.american.edu!howland.reston.ans.net!spool.mu.edu!sdd.hp.com!col.hp.com!news.dtc.hp.com!hpscit.sc.hp.com!hplextra!otter.hpl.hp.com!otter!sfk
  7. Newsgroups: comp.lang.pop
  8. References: <C1CAwq.8up@cs.bham.ac.uk>
  9. Lines: 64
  10.  
  11. Following on from Chris's comments about "vals" and "vars".  
  12. > [Quickly scanning the Pepper code to hand, I see more than 750 ``val''s 
  13. > and less than 250 ``var''s. These figures are distored in favour of
  14. > ``var'' [deleted]
  15.  
  16. Even this is an underestimate in the ratio between val/var, which I reckon
  17. is probably more like 10:1 rather than the noted 3:1, because Chris omitted
  18. to mention that all input locals and loop variables are declared as val
  19. by default.
  20.  
  21. My own experience of the val/var distinction is extremely positive.  Here's
  22. a (elderly) example drawn from $popautolib (the comments are my own).
  23.  
  24.     define global countitem(file) -> x;       ;;; global is soon to be obsolete
  25.     lvars c white = false, file x = 0;        ;;; obsolete syntax.
  26.         discin(file) -> file;
  27.         until (file() ->> c) == termin do
  28.             if strmember(c, '\s\t\n\r') then
  29.                 unless white then
  30.                     x fi_+  1 -> x;           ;;; not safe for v. large files
  31.                     true -> white             ;;; (greater than 1Gb)
  32.                 endunless;
  33.             else
  34.                 false -> white
  35.             endif;
  36.         enduntil
  37.     enddefine;
  38.  
  39. In the above code, it is not obvious what variables are playing the role
  40. of state variables and which are just placeholders for intermediate
  41. quantities.  Here's the same code in a "Pepper" style.
  42.  
  43.     define countitem( file ) -> var x;          ;;; The var declaration makes
  44.         0 -> x;                                 ;;;  dubious practice stand 
  45.         false -> var white;                     ;;;  out.
  46.         for c from_repeater discin( file ) do   ;;; Here, c is a -val- !
  47.             if strmember( c, '\s\t\n\r' ) then
  48.                 unless white then
  49.                     x + 1 -> x;
  50.                     true -> white
  51.                 endunless;
  52.             else
  53.                 false -> white
  54.             endif
  55.         enduntil
  56.     enddefine;
  57.  
  58. Note how the 2 var declarations pin-point the genuinely changing variables.
  59. Also note that the assignment ``discin( file ) -> file'' has been removed
  60. using the new loop syntax for repeaters.  (This is just a more modern
  61. phrasing of the same idiom.)  The val/var annotations, to my mind,
  62. really enhance the look of the procedure for the reader, provide useful
  63. protection for the writer, and even make the compiler writer's job
  64. slightly easier.
  65.  
  66. In this example there is a high proportion of 'var-types' (2 of 4) because the
  67. problem chosen deals with state change.  This reflects the coding style.
  68. More commonly, we find libraries such as flatten, gensym, pipein/out, 
  69. which, as it happens, contain no variables that need to be var-type.
  70.  
  71. I've become a convert to the Pepper style of declarations.  I believe
  72. that once people try them, they'll all be clamoring for them!
  73.  
  74. Steve
  75.