13 Apr 2002 mslicker   » (Journeyer)

raph, C is generally a clumsy language. Take a simple function in C, say the power function:
int power (int x, int p) {
    int r = 1;
    while (p--) r *= x;
    return r;
}
In colorForth we would do this (without color):
power 1 swap for over * next nip ;

This example does not tell the whole story however. Where in Forth we can extend the semantics of the language. C is stuck with the same syntax and semantics, which become increasingly clumsy as programs become complex. My example in C is very generous in this reguard. Look some object oriented C to see my point more fully.

Anyway, I'm not trying to bash C, only providing a counter example to the argument that programming in imperative language is necessarily ugly, or unelegant when compared with functional languages.

I do program in functional languages, but only ocationally, when I intend to run the program once or infrequently.

When writing system code or application code, it is really worth the effort to take the time and craft the code.

--- Update

Forgot the case when the power is zero. Modified the code. Note the colorForth code would need a more semantically complex "for next", since "for next" only handles a positive number of iterations.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!