Loading ...
Sorry, an error occurred while loading the content.

Is dc(1) a concatenative language?

Expand Messages
  • Nick Forde
    ... Hi Ben, I m not sure I should admit to this but in my early Unix days I used dc a lot and got to quite like it. Well at least I liked the RPN notation and
    Message 1 of 16 , Apr 30, 2004
    • 0 Attachment
      Ben wrote:
      > Speaking of experimenting with concatenative languages, I've been
      > playing around with writing dc(1) macros lately and got to wondering if
      > anyone else has any thoughts about it as a concatenative language -
      > starting, I suppose, with whether or not it /is/ one.

      Hi Ben,

      I'm not sure I should admit to this but in my early Unix days I used
      dc a lot and got to quite like it. Well at least I liked the RPN
      notation and ability to write scripts.

      I'm not sure whether it classifies as a concatenative language but if
      my memory serves me correctly it does has an evaluation command 'x' and
      the ability to define macros. I think very limited function composition
      is also possible but only through the definition of nested macros. So
      maybe it is? Early implementations of the bc(1) calculator, which
      uses a c like syntax, simply compiled bc scripts into dc format.

      Here are a couple of my very old scripts which illustrate what is
      possible. The first draws a Mandelbrot set and the second Sierpinskis
      gasket.

      #!/usr/bin/dc
      [[nickf@nospam_.com]n17lc+sc0.6375la+sa]sR[lpan]sP[2lx*ly*lb+si]sK
      [0sr0si0snlCx46lm-splPx0.03750la+salc1+scll12=Qlc80>O]sO[lc40=R]sQ
      [lnsmlIxlKxlJxln1+snln15>C]sC[15sn]sD[lrsxlisylxlx*lyly*-la+sr]sI#
      [lxlx*lyly*+4<D]sJ[_2sa0sclOx0.096lb+sbll1+slll25>L]sL_1.2sb0sllLx

      #!/usr/bin/dc
      [li1+sili;cPlsli2-<p]sp[lzsilz80+sslpx[]p]sP[32li:cli1+si160li<j]sj
      [88lx80+lz-:c]su[32lx80+lz-:c]st[lzlx+1-;clzlx1++;c-0!=ulzlx+1-;clz
      lx1++;c-0=tlx1-sx0lx>X]sX[lPx77sxlXx80lz-szld1-sd_1ld>K]sK[0dsiszlj
      x0si0 79:c88 39:c31sdlKx]dsmx

      Joy examples of both of these have been discussed on this list in the
      past. Of course the Joy versions are much more elegant, and certainly
      easier to maintain :-)

      > I'm sorry to hear that you've been feeling unwell, though glad that
      > it's passing. Get well soon! I look forward to your future writings.

      Yes, get well soon Manfred. I'm missing your insightful comments.

      Best regards,

      Nick.
    • Ben
      ... In that case I probably shouldn t admit that I really quite enjoy scripting in dc and wrote a short introductory article[1] about it as I found my footing
      Message 2 of 16 , Apr 30, 2004
      • 0 Attachment
        Nick Forde wrote:

        > Ben wrote:
        >
        >> Speaking of experimenting with concatenative languages, I've been
        >>playing around with writing dc(1) macros lately and got to wondering if
        >>anyone else has any thoughts about it as a concatenative language -
        >>starting, I suppose, with whether or not it /is/ one.
        >
        >
        > Hi Ben,
        >
        > I'm not sure I should admit to this but in my early Unix days I used
        > dc a lot and got to quite like it. Well at least I liked the RPN
        > notation and ability to write scripts.

        In that case I probably shouldn't admit that I really quite enjoy
        scripting in dc and wrote a short introductory article[1] about it as I
        found my footing in the language!


        > I'm not sure whether it classifies as a concatenative language but if
        > my memory serves me correctly it does has an evaluation command 'x' and
        > the ability to define macros. I think very limited function composition
        > is also possible but only through the definition of nested macros. So
        > maybe it is?

        My main stumbling point is that it doesn't seem possible for
        functions to return other functions, since dc has - as far as this
        neophyte can tell - extremely impoverished string-handling capabilities
        (and there doesn't seem to be any meaningful distinction between strings
        and macros).

        The only idea I had to circumvent this was a dc program that
        constructs programs by manipulating a numeric representation of itself
        plus the newly-defined function, then invokes a new instance of dc and
        feeds it that environment and the additional program.

        > Early implementations of the bc(1) calculator, which uses a c like
        > syntax, simply compiled bc scripts into dc format.

        I've heard that from many sources, but so far I haven't been able to
        track down any open-source implementation of bc with this execution
        model. I've read through the source code for GNU bc and dc and
        discovered nothing revelatory. I feel rather silly asking this, but can
        anyone point me toward the source for a bc implementation that /does/
        compile down to dc, and/or a the source for a non-GNU dc so I can
        compare and contrast?

        James P. Howard claims to have implemented a version of dc based
        around the gmp library, but I cannot find the source to it anywhere;
        Greg Ubben implemented dc in sed, but reading that is an exercise in
        masochism.

        I want to write a front-end to dc to allow the definition of
        functions and variables with names longer than a single character
        (written, of course, in dc), but so far I've been stymied by the
        aforementioned string awkwardness.


        > Here are a couple of my very old scripts which illustrate what is
        > possible. The first draws a Mandelbrot set and the second Sierpinskis
        > gasket.
        [snip]

        /Nifty/! Thank you kindly for the fascinating examples!


        > Joy examples of both of these have been discussed on this list in the
        > past. Of course the Joy versions are much more elegant, and certainly
        > easier to maintain :-)

        Wholeheartedly agreed! :)


        Thanks for your response and pointers; they've been interesting and
        helpful!

        Ben

        -
        [1]:
        http://cpe000103c34069-cm014300001653.cpe.net.cable.rogers.com/weblogs/ben/programming/dc/Prime-Sieve-in-DC.writeback
        --
      • John Cowan
        ... You want the 7th Edition source (now available under a close variant of the BSD license), which is online in many places. One of them is
        Message 3 of 16 , May 1, 2004
        • 0 Attachment
          Ben scripsit:

          > > Early implementations of the bc(1) calculator, which uses a c like
          > > syntax, simply compiled bc scripts into dc format.
          >
          > I've heard that from many sources, but so far I haven't been able to
          > track down any open-source implementation of bc with this execution
          > model. I've read through the source code for GNU bc and dc and
          > discovered nothing revelatory. I feel rather silly asking this, but can
          > anyone point me toward the source for a bc implementation that /does/
          > compile down to dc, and/or a the source for a non-GNU dc so I can

          You want the 7th Edition source (now available under a close variant
          of the BSD license), which is online in many places. One of them is
          http://mirror.cc.vt.edu/pub/projects/Ancient_Unix/PDP-11/Trees/V7/usr/src/cmd/bc.y

          The Caldera (now SCO Group) license is at
          http://www.tuhs.org/Archive/Caldera-license.pdf .

          Note that it's Yacc source with embedded C. To get it running on modern
          Unix, I needed to make these changes:

          Change all calls to exit() to exit(0)
          Change "int in;" in line 19 to "FILE* in;"

          So after "yacc bc.y; gcc -o my-bc y.tab.c; ./my-bc -c" I could type in
          bc statements and watch it spew the corresponding dc code!

          The man page is at
          http://mirror.cc.vt.edu/pub/projects/Ancient_Unix/PDP-11/Trees/V7/usr/man/man1/bc.1
          as you might expect.

          --
          "They tried to pierce your heart John Cowan
          with a Morgul-knife that remains in the http://www.ccil.org/~cowan
          wound. If they had succeeded, you would http://www.reutershealth.com
          become a wraith under the domination of the Dark Lord." --Gandalf
        Your message has been successfully submitted and would be delivered to recipients shortly.