Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I would strongly favor Forth over Joy, but either way concatenative languages are really worth exploring. It seems fair to compare Forth with C, as both deal with hardware and memory rather intimately. Compare a C implementation of a Fisher-Yates shuffle:

  void shuffle(int *array, int n) {
    int i, j, tmp;
    for (i = n - 1; i > 0; i--) {
      j = rand_int(i + 1);
      tmp = array[j];
      array[j] = array[i];
      array[i] = tmp;
    }
  }
With the same algorithm in Forth:

  : shuffle
    1- for
      i 1+ random over +
      over i +
      swap@
    next
  ;
The types, intermediate variables and many of the unnecessary details of the implementation fall away. It's an aesthetic distinction, since we're really trading these for "stack noise" words like over and i, but I find it very pleasing.

C is normally considered an extremely simple, lightweight language. I think the main lesson Forth has taught me is that we can design much simpler languages in the same space without sacrificing power and flexibility.



C is normally considered an extremely simple, lightweight language. I think the main lesson Forth has taught me is that we can design much simpler languages in the same space without sacrificing power and flexibility.

But perhaps at the expense of readability. Forth-like languages look quite elegant in small examples but C's more explicit syntax scales much better to larger code bases, IMO.


Could you please explain the Forth sample? For instance, which variable is the array?


Sure thing.

  : shuffle
This starts a declaration for our word 'shuffle'. This implementation expects the address of the array on the stack with the length of the array on top. Some Forth programmers would leave a comment to this effect like so:

  ( addr len -- addr )
Anything in parentheses is a comment, and this shows the contents of the stack we want as input (read from right to left) before the '--' followed by the contents of the stack we'll get as output. (We leave the array address on the stack. If we want to be void like the C example, add a drop to the end of this definition to discard it.)

  1- for
Subtract one from the length given and begin a for...next loop. This will take the length value off the stack and repeat everything up to the matching next n+1 times. Equivalent to:

  for(int z=n; z >= 0; z--) {}
Our stack now looks like this:

  ( addr )
While we're in the loop, a copy of the loop index can be pushed onto the stack via the word i. This actually accesses a secondary stack, so if you nest loops you can obtain progressive indices by using i' and j (or j and k, depending on your Forth dialect.)

  i 1+ random
Add one to the loop index and generate a random number between 0 and this value, exclusive. Our stack now looks like:

  ( addr rand_int(i+1) )

  over +
Over pushes a copy of the second element of our stack. This sequence adds the address of the array to the random index, leaving the address of that cell of the array. Our stack now looks like this:

  ( addr (rand_int(i+1)+addr) )

  over i +
Make another copy of the array address and add the loop index to it. The stack now looks like this:

  ( addr (rand_int(i+1)+addr) (i+addr) )

  swap@
This is a helper word which swaps the contents of two memory addresses. It should be easy to see that this will swap array indices for us in exactly the same way the C code did.

  next ;
Close the loop and terminate the word definition, respectively.


Whoa..I'm definitely adding Forth to my "to-learn" list. The concept of a stack-based programming language is completely alien to me and would definitely be worth exploring further. On a side note, is it just me or is Forth's readiblity really low?

[Thanks a lot by the way!]


My pleasure. :)

It takes some time to get used to dealing with the stack. The stack plus the fact that there aren't many "structural" symbols like the parentheses and curly braces of ALGOL-derived languages can certainly make Forth harder to skim at first. On the other hand, idiomatic Forth code should be factored into many very small definitions. Aggressive decomposition wins back some readability since everything will be in tiny bite-sized chunks, and it promotes reusing code too. I think personal preference is part of it.

If you're serious about learning more, I'd recommend Leo Brodie's Starting Forth: http://www.forth.com/starting-forth/sf1/sf1.html

I hate plugging my own projects, but if you check out my profile I also have several Forth-related things on github.


Another stack based language worth learning might be Postscript (even if only to make your coworkers wonder why such a small file takes such a long time to print: http://warp.povusers.org/MandScripts/ps.html or http://www.physics.uq.edu.au/people/foster/postscript.html )


It's implicit on the stack.


Ah, thanks :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: