Monday, 3 January 2011

You know you're not a real programming language when...

So, you want to do this:

myf = @(key, dir) if (ls('dir')) do_store(key, dir) end;

But you can't, as the syntax is only:

myf = @(args) expr

And expr can't be a conditional.

What you can do, to fake this, and display to the world that you are covered with infectious language lice, is hide the if statement inside an eval() call.

myf = @(key,dir) eval('if (ls(dir)); do_store(key, dir); end');

This, of course, only works for some use cases.

Now, most real languages have a ? : operator, so this isn't even an issue, but most languages with anonymous functions (lambdas, whatever) let you specify a whole code block, and even let you run it over multiple lines.

my $func = sub { ... };

If matlab let you define script local functions, this would also be less of a problem, as you could put the function body in the script file and reference its name, but you can't.  Nor can you access private/ functions from a script file.  Madness! And people wonder why scientists write disturbing code.  We really have no choice, sometimes.

1 comment:

  1. This post changed my life. Thank you. I HAVE always wondered why scientists write disturbing code.