This time with a little changed rules (IMHO, I tried to make it much more clear).
The aim is to write a program that behaves as a simple expression calculator.
Set of supported operations: +, -, *, /, ^. It must support a correct order of operators, and grouping subexpressions with brackets (). If incorrect expression is given, it should print "error". It must not support anything besides this.
The criteria are simple: who creates the shortest program for the requirements wins. Comments and whitespace don't count. You can use whatever language you wish (this is a Lounge section), but you must not use any external, non-standard libraries.
Does it have to loop?
Is "+2" a valid expression?
Should "5/0" print "error"?
Do you intent to evaluate programs by counting the number of symbols used?
Also, I feel that this would measure not the skill of programmer or the expressiveness of the language, but the size of the standard library. In Ruby puts eval gets would be pretty close to the solution and I'm sure this isn't the kind of solution you were expecting (To be honest I have no idea whether "eval" counts as part of the language or the standard library. It's kind of hard to tell sometimes..).
Of course, if there was no restriction on the use of libraries, I'd write one myself for this task and if you couldn't use any libraries other than io, then .. I'm not sure what would happen. Ruby would either remain unchanged or become totally useless.
Perhaps with some effort we could adopt better restrictions. I don't have any clue what that could be though.. If we don't manage to do that, we should stick with a single language. If you don't want that either, I'll just write my own language with a keyword "e" which performs the task and win.
@ultifinitus
lol. Did you test that? Line 8 will probably segfault causes an assertion failure and you have several =s instead of ==s..
It is not a way to spark a language debate. It is rather to spark a compiler/interpreter construction debate. The solution with eval does not count, because it doesn't conform to the "It must not support anything besides this" rule - which was added just to rule out all the "my language has a function that does that" solutions, which would not be interesting at all. Anyway, using "eval" function in a production code to evaluate something given by the user, is an extremely risky idea, everyone from the QA would consider a vulnerability.
Should "5/0" print "error"?
5/0 should return +Infinity, but can also return error. I don't mind. Whatever is easier/shorter to code. All numbers should be treated as floating point.
Is "+2" a valid expression?
"+2" - it is correct, because the "+" is a part of a number. However there is no need to support unary + and -, so this can yield error: "-(2+3)".
Does it have to loop?
Yes.
The length of the code will be counted by counting symbols (lexems) used. I think it is a good measure, because it doesn't force anyone to shorten variable and funciton names, rendering the code unreadable.
@ultifinitus: nice try, but these were only examples, not specification. So your code obviously doesn't conform.
My point was not that eval solves the problem, but that you don't "rule out all the "my language has a function that does that" solutions" well enough (I'm not suggesting that using eval is a good idea either).
The length of the code will be counted by counting symbols (lexems) used. I think it is a good measure, because it doesn't force anyone to shorten variable and funciton names, rendering the code unreadable.
So the code can be as long and complex as we want, provided we keep the number of symbols we define to a minimum?
So just to clarify:
1) using the conditional operator in place of if statements will not be considered to make the program shorter
2) reusing the same old variable rather than creating a new one will make the code shorter
3) reducing the number of variable names used by any other means no matter how contrived and complicated would result in the code being considered shorter?