Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

dc "programming"

Name: Anonymous 2012-07-25 8:52

The UNIX program dc is quite interesting calculator. The man page says: "dc  is  a reverse-polish desk calculator which supports unlimited precision arithmetic."

For starters, a word about this thread: This is not a serious discussion about dc program, but more like how to have fun with dc.

After having some boring time at work, I read the man page of dc for fun. Before, I thought this is quite boring and useless program, but now I feel exited about it. The nice thing I found about it, is that it seems you can do pretty much everything with it.

After messing around a bit, I thought I'd implement fibonacci numbers in it. Here's the result:

30 1-sc1pp[lfx1+dlc>l]sl1dSFSF[LFLFrdSF+pSF]sfllx

To run this, you can run
echo '30 1-sc1pp[lfx1+dlc>l]sl1dSFSF[LFLFrdSF+pSF]sfllx' | dc
in your terminal.

This prints out 30 first fibonacci numbers. I think it's pretty short, and it could be much shorter, though making so might make things harder to read.

What I would like to see in this thread, is other "programs" written in dc. It could be better implementation of fibonacci numbers, or something completely different. I might try doing prime numbers next.

BTW, here's an overly commented version from fibonacci above (for those who don't understand anything)

# push 30 to stack, the count of fibonacci numbers to be calculated
30

# store count-1 (29) to register c. That's our loop counter register
1-sc

# push 1 to stack. we use it as our loop counter
1

# as we have 1 in stack, we might as well print the first two fibonacci numbers :P
pp


# create macro for our loop system
[
  # execute macro f
  lfx
  # increment top of stack (loop counter) by one
  1+
  # compare top of stack to count register (c), execute macro in register l if c is larger
  dlc>l
]
# now, we have macro text on the top of our stack, we save it to register l
sl

# create macro that the loop executes (which goes to register f)
# first, initialize own stack for this. that stack is in register F
# at first it has 1 and 1 in it's stack. the numbers in that stack are
# the two last numbers of fibonacci sequence.
1dSFSF

# begin macro
[
  # get the numbers from F stack to main stack (r reverses them, so they are in same order)
  LFLFr

  # put the top of the main stack back to F stack, but don't change main stack
  # this is the second last number of sequence
  dSF

  # add the two numbers in main stack to get new number for sequence
  +

  # print it
  p
  # store it to stack F. stack F now has two numbers again
  SF
]
# store macro to f
sf

# execute our loop macro in register l
llx

Name: Anonymous 2012-07-26 8:50

>>13
See, that's exactly the problem with you UNIXers. You consider it normal to hack around the insanity of your system. That's also why most usable UNIX programs and libraries are the ones whose designers flushed the UNIX philosophy down the toilet (example: GNU emacs, modern OpenGL, Mozilla Firefox).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List