www.digitalmars.com         C & C++   DMDScript  

D - Two syntax ideas (sadly not mine)

reply "Ben Cohen" <bc skygate.co.uk> writes:
I found a book on programming language design which you can download from
here:
  http://cseng.aw.com/book/related/0,3833,0805311912+20,00.html#top

It contains two ideas which I found particularly intriguing, although I
don't know whether they would be worth adding to a language, overall.


"Power Loops" are a cross between "for" loops and recursive functions. 
E.g., use "nest" to say "this for loop can be at different levels" and
"deeper" to go to the next level:

   variable
      Queen : array 1 ..n of integer;

   nest Column := 1 to n
      for Queen[Column] := 1 to n do
         if OkSoFar(Column) then
            deeper;
         end; -- if OkSoFar(Column)
      end; -- for Queen[Column]
   do
      write(Queen[1..n]);
   end;

Of course, they would only rarely be useful.


You can also use dimensions as a kind of meta-type to help type-safety
and make sure that you don't make bad conversions.  E.g.:

   dimension
      area = distance * distance;
      velocity = distance / time;
   constant
      mile = 5280 * foot;  -- foot is predeclared
      acre = mile * mile / 640;
   variable
      d1, d2 : distance real;
      a1 : area real;
      v1 : velocity real;
   (etc.)

This might be useful in science and engineering.  In D it could be
achieved using (string) typedefs and conversion macros so I don't imagine
it would be worth adding.


What do you think about these?
Nov 08 2001
parent "Walter" <walter digitalmars.com> writes:
Dimensional programming probably would be interesting for science and
engineering apps. -Walter

"Ben Cohen" <bc skygate.co.uk> wrote in message
news:9secuk$2oja$1 digitaldaemon.com...
 I found a book on programming language design which you can download from
 here:
   http://cseng.aw.com/book/related/0,3833,0805311912+20,00.html#top

 It contains two ideas which I found particularly intriguing, although I
 don't know whether they would be worth adding to a language, overall.


 "Power Loops" are a cross between "for" loops and recursive functions.
 E.g., use "nest" to say "this for loop can be at different levels" and
 "deeper" to go to the next level:

    variable
       Queen : array 1 ..n of integer;

    nest Column := 1 to n
       for Queen[Column] := 1 to n do
          if OkSoFar(Column) then
             deeper;
          end; -- if OkSoFar(Column)
       end; -- for Queen[Column]
    do
       write(Queen[1..n]);
    end;

 Of course, they would only rarely be useful.


 You can also use dimensions as a kind of meta-type to help type-safety
 and make sure that you don't make bad conversions.  E.g.:

    dimension
       area = distance * distance;
       velocity = distance / time;
    constant
       mile = 5280 * foot;  -- foot is predeclared
       acre = mile * mile / 640;
    variable
       d1, d2 : distance real;
       a1 : area real;
       v1 : velocity real;
    (etc.)

 This might be useful in science and engineering.  In D it could be
 achieved using (string) typedefs and conversion macros so I don't imagine
 it would be worth adding.


 What do you think about these?

Nov 08 2001