Saturday, January 5, 2008

A language that saves you some typing.

A couple of days ago I was whining to my friend about the mainstream programming languages - they are so *verbose*.

consider even one of the lowest levels, C:

int add_two(int to_what)
{
return 2 + to_what;
}

int main(int argc, char *argv[])
{
int another_two = 2;
int result = add_two(another_two);
printf("Result is %d\n", result);
return 0;
}

Now, how many times do you *really* need to declare the types of things, and how many of them are not really necessary ?

Iet's see. add_two should know that adding two integers is integer - so the declaration of the return type is unnecessary. another_two is assigned 2 - which is integer, so it is integer. add_two is called with this resulting integer - so the declaration of the parameter type there is also redundant. result is assigned the return value from the integer function add_two - so its type declaration is also not needed.

printf, in turn - should not bother to put the "d" into the format string. Iinteger argument should obviously should be printed as integer. main() knows about its arguments from the C library - and we don't use them anyway.

So we could simplify this down to:

add_two to_what : to_what + 2;

main * : another_two = 2,
result = add_two(another_two),
printf("Result is %\n", result),
0;

we got rid of *all* the type declarations - and yet, this description is fully type-strict.

Of course, all the cool stuff has already been done - and the above is quite close to the syntax of OCaml. What is interesting as well - OCaml has quite good performance as well. (Haskell has some similar ideas and is much more powerful - being a lazily evaluated functional language, but is slower).

Of course, being quite an esoteric stuff, google coding with those is much more painful compared to javascript...

(And indeed I should have come up with some less indecent code example, but a bit tired after a 24h low-level hacking marathon with GDB :) Instead I give you a much better link - http://eigenclass.org/hiki/legitimate-microbenchmarks.

A brilliant site all together. very nice read.

No comments: