2012-01-24

Guess the programming language with array and dictionary addition

Guess the programming language.

  1. In which programming language are all of these true?
    • [] + [] is an empty string.
    • [] + {} is a string describing the empty object.
    • {} + [] is 0.
    • {} + {} is not-a-number.
  2. In which programming language are all of these true?
    • [] + [] is an empty list.
    • [] + {} raises TypeError.
    • {} + [] raises TypeError
    • {} + {} raises TypeError.
  3. In which programming language are all of these true?
    • [] + [] is an empty list.
    • [] + {} raises TypeError.
    • {} + [] raises NoMethodError
    • {} + {} raises NoMethodError.
  4. In which programming language are all of these true?
    • [] + [] is a positive number, e.g. 19806432.
    • [] + {} is a positive number, e.g. 20142304.
    • {} + [] is a positive number, e.g. 58300640.
    • {} + {} is a positive number, e.g. 27597024.

8 comments:

airween said...

1: donno' :(
2: Python
3: Ruby
4: Fuckin' Perl? :)

zsbana said...
This comment has been removed by the author.
zsbana said...

When I tried these expressions in Javascript, it turned out that all of them result in a string, which matches none of your questions. I also suspected PHP for a while, but it seems none of those are valid syntax in PHP.

zsbana said...
This comment has been removed by the author.
zsbana said...

Question 1 is driving me mad, so I'm posting some negative results.

There's a remote chance that these expressions could be syntactically valid in some APL dialect where curly braces make a lambda expression, +[] could be accepted as addition with a meaningless and unnecessary axis specification, and the leading [] could be accepted as del editor commands, in which case they could match the description of question 1.

There's also a remote chance that these expressions are valid syntax in some assembly language and give these results.

All expressions are valid in the J language, all evaluate to a verb, with ([] + []) being a more poetic way to write just (+"_) or (+[]), and the rest of them does not seem very useful.

All expressions return a string in Javascript.

In prolog, using the expressions as prolog syntax on the second argument of the is operator, the is operator finds the arithmetic expression a syntax error in all cases.

All expressions return an error in Postscript because the name + is undefined.

The expression {} + {} is valid in lua, but raises the error "attempt to perform arithmetic on a table value"; the other expressions are syntactically invalid.

The expression {} + {} seems to be syntactically valid in s-lang, and gives the error "List_Type with 0 elements"; but the other expressions give the error "Empty inline-arrays not supported".

The expression {} + {} is valid in metafont, because {} is an unknown name which is thus treated as an unbound numeric variable, but the rest of the expressions are syntactically invalid.

All expressions give the type error 'can't use empty string as operand of "+"' when passed as a literal in TCL with the expr command.

All expressions give the type error "expr: non-integer argument" in expr (the old unix tool).

The expression [] + [] gives a stack underflow in dc.

The expression [] + [] is a type error (missing class instance) in Haskell, the other expressions are syntactically invalid.

I believe [] + [] and possibly {} + [] are valid expressions in Pascal ([] is a set constructor and {} is a comment), but {} + {} and [] + {} are syntactically invalid.

TeX or LaTeX doesn't have arithmetic expression syntax, and as commands, these expressions don't do anything fancy.

I believe all four expressions are syntactically invalid in the following languages (but some of these I haven't actually tested): C, C++, BASIC, bc, PHP, bash (as a numeric expression), zsh (as a numeric expression).

I don't know anything about the behavior of these expressions in at least the following languages: Standard ML, OCaml, Java, Fortran, Ada, C#, Pike, K (that stupid commercial APL-alike).

Perl, Python2, Python3, Ruby1.8, and ruby1.9 are covered by the other questions, so they can't match.

I even checked the docs of the muzcat program and the polyglot obfuscation from http://pts.szit.bme.hu/index__hu.html to try to guess what languages you could be thinking of, but I didn't find anything.

zsbana said...

Update on the previous post. It occurred to me that I can check many languages with the online interpreter at http://ideone.com/ , so I did that, and did some other research.

All four expressions are valid in maple, because [] is a list constructor and {} is a set constructor. It turns out that [] + [] returns an empty array (because + does elementwise addition on the arrays), and the other expressions are left unevaluated (symbolic).

Interestingly, all expressions are syntactically valid in Digitalmars's D, as [] is an empty array constructor and {} is a function constructor. It turns out that [] + [] gives the compile time error "Array operation [] + [] not implemented", {} + {} seems to return a function, and the other two are type errors.

All are syntactically valid in Octave, and [] + [] returns an empty array but the rest are type errors. I presume the same is true in Matlab.

The expression {} + {} is valid in Mathematica and returns an empty list (the elementwise addition of two empty lists); the other expressions are invalid syntax.

Similarly, [] + [] is valid in GAP and returns an empty list (the elementwise addition of two empty lists); the other expressions are invalid syntax because braces are used only for array slicing.

All four expressions are syntactically valid in some dialects of Smalltalk, because [] is a function constructor, and {} seems to be some modern syntax extesion though I'm not sure what it does. In any case, all four expressions give an error because the values don't implement a + method.

In Ocaml, [] + [] is a type error, the rest are syntax errors.

All four expressions are syntax errors in Java, C#, Go, and Pike.

I think at least the curly braces are syntax errors in Maxima and in all dialcts of Fortran, but I'm not really sure, so someone should check these.

Someone should also check what the expressions do in Standard ML.

zsbana said...

After consulting with Pts, it turns out the solution for the first riddle is Javascript afterall. There are two reasons why I got a different result. One reason is that javascript is similar to perl in that {} in statement context makes an empty compound statement, so {} + [] in statement context is just that statement followed by an expression statement with a unary plus; but {} in expression context means an empty object constructor, so the expression ({} + []) is a sum. The second difference is that [] + {} actually results in the string "[object Object]" which looked like an empty object to Pts but actually isn't one.

Anonymous said...

1) is javascript, although you misread the output

[06:01:31.778] []+[]
[06:01:31.782] ""
[06:01:35.287] []+{}
[06:01:35.291] "[object Object]"
[06:01:39.852] {}+[]
[06:01:39.856] 0
[06:01:43.559] {}+{}
[06:01:43.563] NaN