Isn't it annoying when you find a piece of code and you don't know what language it was written in? This challenge attempts to somewhat solve this.

Challenge

You will have to write a program that when run in two different languages, will output the string:

This program wasn't written in <language program compiled/interpreted in>, it was built for <other language the program can be run in>!

  • In the output, language names should have official capitalization. eg: CJam, C++

  • Neither program should take any input from the user.

  • When run in both languages, output should be to stdout or equivalent.

  • There should be no output to stderr in either program.

  • You may use comments in either language.

  • Two different versions of the same language count as different languages.

    • If this is done, the program should output the major version number, and if running on two different minor versions, should report the minor version also.

    • You should not use prebuilt version functions (this includes variables that have already been evaluated at runtime).

Example output:

Perl and Ruby:

  • Perl: This program wasn't written in Perl, it was built for Ruby!

  • Ruby: This program wasn't written in Ruby, it was built for Perl!

Python and C:

  • Python: This program wasn't written in Python, it was built for C!

  • C: This program wasn't written in C, it was built for Python!

Python 2 and Python 3:

  • Python 2: This program wasn't written in Python 2, it was built for Python 3!

  • Python 3: This program wasn't written in Python 3, it was built for Python 2!

Python 2.4 and Python 2.7:

  • Python 2.4: This program wasn't written in Python 2.4, it was built for Python 2.7!

  • Python 2.7: This program wasn't written in Python 2.7, it was built for Python 2.4!

This is code golf so the shortest code in bytes wins.

  • 2
    "Two different versions of the same language count as different languages." So tricking like with C pre and past 99 comments is valid? easy ^^ – Zaibis Sep 3 '15 at 13:54
  • 1
    Added it, it feels paradoxical but fits to your rules. – Zaibis Sep 3 '15 at 14:48
  • 1
    No one's doing a Whitespace/Python polyglot? – Not that Charles Sep 4 '15 at 15:22
  • nevermind. Saw the 23 implementation – Not that Charles Sep 4 '15 at 15:26
  • Does relying on the program to be run with a certain interpreter count as input? that is, switching on argv[0]? – cat Oct 28 '16 at 12:10

93 Answers 93

up vote 77 down vote accepted

Foo/CJam, 70 bytes

"This program wasn't written in ""Foo"", it was built for ""CJam"\@"!"

In Foo, as many have found out, it just prints everything in the double quotes, and ignores most other character or does something that doesn't affect the output in most cases. In short, \@ does nothing and the strings are all printed as-is.

In CJam, \ swaps the top two items, and @ moves the 3rd item to the top, which arrange the strings into the right order. And after the program ends, everything left in the stack is automatically printed.

  • 7
    And just for the fun of it, there's a similar 75-byte solution for Fission/CJam: R"This program wasn't written in ""Fission"", it was built for ""CJam"\@'!O – Martin Ender Sep 3 '15 at 13:18
  • 3
    Nice. I had "This program wasn't written in Clip, it was built for CJam!"3{4-_36+e\}/ for Clip/CJam. – Dennis Sep 3 '15 at 13:58
  • 4
    I came into this thread to post a Foo solution, should've realized everybody knows by now how easy it is to write a Foo polyglot. – histocrat Sep 3 '15 at 18:16
  • Is there a link to the Foo programming language spec? – justhalf Sep 4 '15 at 3:30
  • 1
    @ErikE Added some explanation. – jimmy23013 Sep 4 '15 at 17:14

C89/C99, 171 152 136 114 111 107 bytes

Thanks at @Hurkyls, @Qwertiys & jimmy23013 for your hints.

golfed version:

main(){int c=-4.5//**/
-4.5;printf("This program wasn't written in C%d, it was built for C%d!",90-c,98+c);}

ungolfed version:

main()
{
    int c =-4.5//**/
    -4.5;
    printf("This program wasn't written in C%d, it was built for C%d!",90-c,98+c);
}

Little description:

C versions previous C99 just had the multiline comment like this:

/*foo*/

with C99 the single line comment was introduced. like this:

//foo

so if you compile a line like this:

c =-4.5//**/
-4.5;

the for the c99 compiler compiling-related code would be:

c = -4.5 -4.5;

while the for a c89 compiler relevant code would be:

(as the first / isn't part of a comment and therfor treat as operator)

c = -4.5 / -4.5;
  • 9
    +1 for a marvellous answer. A little bit of explanation for those unfamiliar with C would be nice though. – ace Sep 3 '15 at 15:52
  • 4
    @ace I believe this relies on C99-style comments. In the fourth line, notice the //**/. In C89, that's the division operator followed by an empty comment. In C99, // starts a single-line comment, so the rest of the line is blank. Therefore, in C89, it becomes (90-(-4.5/-4.5)), which is 89, while in C99, it becomes (90-(-4.5-4.5)), which is 99. – kirbyfan64sos Sep 3 '15 at 16:06
  • 14
    To save a few bytes, use 188-c instead of c==99?89:99. – Hurkyl Sep 3 '15 at 19:24
  • 1
    I don't think you need the space and the outer parenthesis with the assignment. – Pietu1998 Sep 7 '15 at 4:59
  • 1
    105 bytes – MD XF Nov 21 '17 at 3:24

JavaScript/Ruby, 170 bytes

Might be 2.0 only, doesn't appear to work in at least 2.1.5... Edit: Updates as per advice from @Jordan hopefully it works in a few more versions now!

a='1';c=console=console||eval('def c.log s;$><<s end;c');c.log("This program wasn't written in "+(d=['JavaScript','Ruby'])[b= ~(a=~/1/)]+', it was built for '+d[b+1]+'!')

Abuses the ~ operator in that Ruby will treat =~ as a regex match returning the position of the first match in the string (0), but JavaScript will treat it as = ~/1/ which is -1 (since /1/ is converted to NaN for numeric operations, which has 0 value).

  • This answer does not work for me in Ruby 2.1.5. I get: NoMethodError: undefined method `log' for :log:Symbol – EMBLEM Apr 5 '16 at 1:57
  • @EMBLEM My testing was carried out on Ruby 2.0 built into OS X, I haven't tested anything beyond that, but I'll certainly add a comment indicating it might well be broken in other version! – Dom Hastings Apr 5 '16 at 6:07
  • In more recent versions of Ruby, a method definition returns the name of the method as a symbol, so your eval is returning :log. I think you could fix it by putting ;c after end. The =~/= ~ trick is great, though! – Jordan Sep 1 '16 at 18:05
  • You could also save. A few bytes by using $><<s instead of puts s and using double-quotes so you can drop the backslash in wasn\'t. – Jordan Sep 1 '16 at 18:10
  • @Jordan Thanks! Not sure how I didn't spot the removal of the \' with double quotes though, and $><< means I can apply the fix you mentioned and keep the same byte count! – Dom Hastings Sep 2 '16 at 7:27

Python 2/Python 3, 92

Uses the "standard" Python version check (integer vs. float division).

print("This program wasn't written in Python %d, it was built for Python %d!"%(3/2*2,4-3/2))
  • 3
    This was talked about in the sandbox and the consensus was to allow this sort of thing – Blue Sep 3 '15 at 10:08
  • 14
    @flawr Would Python and Cobra be allowed? ;) – Beta Decay Sep 3 '15 at 10:11
  • 7
    @flawr Python is actually named after the comedy ~~group~~ gods, Monty Python. – Mutantoe Sep 3 '15 at 20:35
  • @Mutantoe It may appear to you that "Monty" not being a name of an animal, the pun wouldn't work. – Pierre Arlaud Sep 4 '15 at 7:46

Lua/C - 182 164 bytes

#if 0
print"This program wasn't written in Lua, it was built for C!"--[[
#endif
main(){printf("This program wasn't written in C, it was built for Lua!\n");}/*]]--*/

Takes advantage of the feature where Lua treats a hash mark on the first line as a comment to allow for Unix shebangs. Otherwise wraps the other language's comments in its own comments.

To shave bytes, I rely on implicit behavior that only emits warnings in GCC and Clang: implicit declaration of int for main and implicit definition of printf.

  • 3
    Very cleverly done! – Cows quack Sep 4 '15 at 4:31
  • 2
    If removing "include<stdio.h>" is too extreme, I'll revert the answer. – benpop Sep 4 '15 at 8:13
  • 1
    Why not use the // comment in the C part? Saves 2 bytes. – BrainStone Oct 26 '16 at 1:06

Fishing/><> 233 217 bytes

v++C-CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC+CCCCCCC-CCCCCCCCCCCCCCCCCCC+CCCCCC
\   "This program wasn't written in ""><>"  ", it was built for Fishing!"
>r!`ol?!;32.                         Fishing                     ><>!`N

Fishing is a language based on a fisherman walking around catching fish. To make a program in this language who first have to define a dock on which he walks around. The dock only provides control flow to a program. The dock in this program is:

v++C-CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC+CCCCCCC-CCCCCCCCCCCCCCCCCCC+CCCCCC

Whenever the C command is found, the fisherman throws out his line to catch an instruction. The + and - instructions decrease and increase the length of his line respectively. v changes his throw direction to downwards. The fish that he catches in this program are:

`This program wasn't written in Fishing, it was built for ><>!`N

><> is a language based on a fish moving through the water. The v command starts the fish moving downwards, where it is then reflected to the right with the \ command. Everything between quotes is pushed onto the stack. After the string is pushed onto the stack, the fish wraps around to the other side where it is reflected downwards by \. It then prints out the contents of the stack with:

>r!`ol?!;32.
  • You can reduce your code by 13 bytes by putting your print loop for ><> in the whitespace on the third line (which I don't think will interfere with the fishing part, although I don't know that language). I'm not sure how this comment will handle the whole code, so here's a hastebin link: hastebin.com/quqinozizi (I may have messed up the spacing on the third line but I think that everything is aligned properly). – cole Sep 5 '15 at 20:00
  • @Cole It does work. In fact your suggestion actually removes 16 characters. – TheNumberOne Sep 5 '15 at 21:13
  • 12
    Fish and fishing polyglot... possibly the first of its kind! – Mark K Cowan Sep 7 '15 at 18:28
  • this is so funny what! – D.Tate Sep 10 '15 at 21:58

23/Malbolge, 5688 bytes

                    bCBA@?>=<;:987
                                                                                        6543210/.-,+*)
                                                                                          ('&%$#"!~}|{zy
                                                                                               xwvutsrqponmlk
                                                                                                  jihgfedcba`_^]
                                                                                     \[ZYXWVUTSRQPO
                                                                                               NMLKJIHGFEDCBA
                                                                                    @?>=<;:9y76543
                                                                210/(L,l*)(!E}
                   |B"!~}|{zyxwvu
                                                                                                     tsrqponmlkjiha
                                                                                                  fed]#a`_^]?zZY
                                                                                         XWVUTSRQ3ONMLK
                   JIHGFEDCBA:^>=
                                                                                                       <;:98705.R21q/
                                                                                               .-,+*#G'&%${"!
                                                                                            x>|{zyxwYutm3k
                                                                                                        ponmlkjihg`&^c
                                                                                     ba`_^]\[ZYXWVO
                   sSRQPONMLEi,HG
                                                                                                      FEDCBA@?>=6Z:9
                                                                                    y76543210/.-,+
                                                                                                          *)('&%$#"y?w|u
                   ;sxwvutm3qSonm
                                                                                                       fkjiha'edcba`_
                                                                                            ^]\[ZYXWVUTSRQ
                   PONM/EiIHGFEDC
                               BA@?>7[;:987w5
                                      432+O/o-,%I)('
                                     &}$#z@~}|{zsxw
                   vutsrqponmlkji
                                                                                                 ha'&dFba`_^]\U
                                                                                            yYXWVUTMRQPONM
                   LKDhH*F?DCBA@?
                                                                                                 8\<;:98765432r
                                                                                        0/.-&J*)('&f$#
                                                                                                       "!~}|{zyxwvuts
                                                                                                       rqj0nmOkjihaf_
                                                                                            %cE[!_^]\[=SwW
                                                                                                     VU7SLpPONMLEJI
                                                                                                          HAeEDC%A@?>=<;
                   :9876543210/.-
                                                                                                       ,+$H('&}${A!xw
                          ={]yxwvutsrk1o
                                                                                                 nmOejib(fedcE"
                                                                                                      `_^]?[ZYRvVUT6
                                                                                     RKo2HMLKJIHAe
                                                                                                           EDCBA@?>=<;:9
                    87w5432+O/.-,
                                                                                                 +*)('&%e#"y?w
                                                                                     |{zs9wvun4rqp
                                                                                                      onmlNjib(fedc
                                                                                           ba`_^]\[ZYXWV
                                                                                                   8TMqKPONMLKDh
                                                                                                      +GFEDCB;_?>=<
                                                                                                    ;:9y7654321*N
                    .-,+*)('&f|{A
                                                                                                       !~}|{]yxwvo5s
                                                                                             rqpinmlkjihg`
                                                                                            &dcbD`_^]\[Tx
                                                                        ;WVUTMRQJnN0F
                                                 KDhH*FEDC<A@?
     >=<5Y92765.R?

Note that the program requires a trailing linefeed. No line contains trailing whitespace, so copy/paste should work just fine.

Verification

To test the Malbolge code in this online interpreter, paste it in the Malbolge code area and click Load/Reset, then Execute.

To test the 23 code in this online interpreter, paste it in the Source area, press Enter to insert the trailing linefeed, type 23 in the Console area (to switch from the default 23.dezsy notation to auto-detection) and click Run Interpreter!.

  • 19
    Dude... it looks like a bomb went off in your code! – D.Tate Sep 10 '15 at 22:01
  • 6
    o_0 how did you write Malbolge code – Pavel Aug 11 '17 at 0:56

Brainfuck/Foo, 769 bytes

-[--->+<]>-.[---->+++++<]>-.+.++++++++++.+[---->+<]>+++.[-->+++++++<]>.++.---.--------.+++++++++++.+++[->+++<]>++.++++++++++++.[->+++++<]>-.--[->++++<]>-.-[->+++<]>-.--[--->+<]>--.-----.[++>---<]>++.[->+++<]>-.[---->+<]>+++.--[->++++<]>-.-----.---------.+++++++++++..+++[->+++<]>.+++++++++.-[->+++++<]>-.-[--->++<]>-.+++++.-[->+++++<]>-.+[->++<]>.---[----->+<]>-.+++[->+++<]>++.++++++++.+++++.--------.-[--->+<]>--.+[->+++<]>+.++++++++.+++[----->++<]>.------------.-[--->++<]>-.+++++++++++.[---->+<]>+++.--[->++++<]>-.-[->+++<]>-.--[--->+<]>--.+[---->+<]>+++.[->+++<]>++.[--->+<]>-.------------.+++.++++++++.[---->+<]>+++.++[->+++<]>.+++++++++.+++.[-->+++++<]>+++.+++[->++<]>.+[--->+<]>++..[--->+<]>----."This program wasn't written in Foo, it was built for Brainfuck!"

An extremely intricate and complex answer... or not.

  • 21
    Dammit, not you again Foo :D – Beta Decay Sep 3 '15 at 8:36
  • 5
    I like how you think. – Pierre Arlaud Sep 4 '15 at 7:47
  • 13
    When running this in Brainfuck, Brainfuck will wait for input because of the , in the text at the end. I believe that goes against the assignment rules. – Simon Forsberg Sep 9 '15 at 20:45
  • 1
    @SimonForsberg it is a long time, but in an interpreter like try it online, and most others I think, , just sets cell to 0, for EOF – Destructible Lemon Dec 11 '16 at 8:25

JavaScript/Haskell, 158 bytes 147 bytes

General idea: sneak each one's comment syntax into the other.

In one line:

u="This program wasn't written in ";v=", it was built for ";j="JavaScript";h="Haskell";{-console.log(u+j+v+h+"!")}//-}main=putStrLn$u++h++v++j++"!"

What this looks like to Haskell:

-- some variable definitions
u = "This program wasn't written in "
v = ", it was built for "
j = "JavaScript"
h = "Haskell"

-- a comment
{-console.log(u+j+v+h+"!")}//-}

-- the main method that does the dirty deed
main = putStrLn $ u ++ h ++ v ++ j ++ "!"

What this looks like to JavaScript:

/* variables can be declared without `var` */
u = "This program wasn't written in ";
v = ", it was built for ";
j = "JavaScript";
h = "Haskell";

/* hey look, an anonymous block! */
{ 
  /* we negate the `undefined` that comes out of console.log */
  -console.log(u+j+v+h+"!")
} 
/* there are two automatic semicolon insertions here:
   one before `}` and one before EOF. */

/* a one-line comment. */
//-}main=putStrLn$u++h++v++j++"!"
  • 3
    alert is golfier. – wizzwizz4 Apr 7 '16 at 15:08

C / Python, 238 chars

This doesn't print 100% exactly what's requested, but quite close.
A reboot of my valentine's day card.

#define def main(){0?
#define print printf(
#define return 0)));}
#define pass 0);

def main():
    print "This program wasn't written in ",
    pass
    print "Python",
    print ", it was built for ",
    print "C",
    return

main();
  • 5
    What does it print if it isn't exact? – Beta Decay Sep 3 '15 at 9:17
  • 4
    That nested printf thing is brilliant... – immibis Sep 3 '15 at 13:09
  • 4
    This is not valid C. Clang complains about def and the : after def main(), and you don't actually open a function body for main. Did you actually try compiling your answer in C? – C0deH4cker Sep 4 '15 at 5:10
  • 5
    The line #define def main(){0? is missing – kay Sep 4 '15 at 13:08
  • 3
    #define return makes me cry a little... – corsiKa Sep 10 '15 at 19:45

Befunge/><>, 141 138 134 133 130 bytes

3 bytes saved thanks to @Cole.

To be exact, I'm using Befunge-98.

\"!><> rof tliub saw ti ,egnufeB"   >" rof nettirw t'nsaw margorp sih"'T>:#,_@'~~~~~~
>l?v"!egnufeB rof tliub saw ti ,><>"^
?!;>ol

Using the facts that:

  • \ is a mirror in ><> and swap in Befunge
  • 'string' is a string in ><> and 'c is a char in Befunge
  • Unless you need it for the Befunge code (which I don't think you do), you can shave off 3 bytes by replacing the bottom line with ?!;>ol – cole Sep 4 '15 at 0:40

C/C++, 136

#include<stdio.h>
int main(){
char*a="++",z=sizeof'c'/2;
printf("This program wasn't written in C%s, it was built for C%s!\n",a+z,a+2-z);
}

Newlines added for formatting. Try it in C or C++.

  • 31
    What if I'm on a platform where int is 2 bytes? Where can I get a C+ compiler? – immibis Sep 3 '15 at 13:08
  • @immibis haha :P but on a serious note the empty struct trick might work: struct{}s;z=2*sizeof s – grc Sep 3 '15 at 16:06

PHP/MySQL, 147 bytes

-- $argc;die("This program wasn't written in PHP, it was built for MySQL!");
SELECT("This program wasn't written in MySQL, it was built for PHP!");
  • 16
    Finally, one I kinda understand. – MikeTheLiar Sep 3 '15 at 14:47
  • 1
    You should be able to get rid of the second line's parentheses, for two bytes fewer: SELECT"This program wasn't written in MySQL, it was built for PHP!"; – msh210 Sep 8 '15 at 17:36
  • 2
    But that will no longer work in PHP - it will cause a parse error. SELECT"..." is not a valid PHP expression. – Razvan Sep 10 '15 at 22:36

Python 3/><>, 177 173 172 167 Bytes

Thanks to @mathmandan for shaving 5 bytes off!

Well this was an experience, and a trying one, too. Any golf suggestions are welcome, since this is pretty long. I tried my best to reuse text, but it was quite difficult.

Technically, it would be Python 3 that this program should output (and I could change that if I didn't meet the specs -- but in the example Python/C output Python was listed).

aa=" ni nettirw t'nsaw margorp sihT\"\""
v="><>!"                 #v   "><>"r~/
a=", it was built for "+v#\a
print(aa[-3::-1]+"Pytho" +"n"+a)
#  .4b;!?lor"!nohtyP r"~/

Try it on an online ><> interpreter and a Python 3 interpreter (the ><> interpreter requires you to input the code manually)

Returns

This program wasn't written in ><>, it was built for Python!

in ><> and

This program wasn't written in Python, it was built for ><>!

in Python.

Explanation (Python)

For the Python side of things, it's pretty simple. Here's the code that we care about (basically the code without comments, which are denoted by a # in Python). Note that in Python \ is an escape character when used in strings, so \" evaluates to " in the string.

aa=" ni nettirw t'nsaw margorp sihT\"\""
v="><>!"
a=", it was built for "+v
print(aa[-3::-1]+"Pytho" +"n"+a)

What we care most about here is the operations performed on the variable aa:

aa[-3::-1]: reverses the string and chops off the quotation marks (thanks to @mathmandan)

The print statement thus evaluates to

"This program wasn't written in " + "Pytho" + "n" + ", it was built for ><>!"

Explanation (><>)

Now we get to the more difficult part. Once again, here's the code with the unnecessary bits removed.

aa=" ni nettirw t'nsaw margorp sihT\"\
                          v   "><>"r~/
a=", it was built for "+v \a

   .4b;!?lor"!nohtyP r"~/

Line 1:

aa=" ni nettirw t'nsaw margorp sihT\"\

aa=         pushes 1 onto the stack (evaluates 10==10, basically)
" ni ... \" pushes the first part plus a \ onto the stack.
\           deflects the pointer downwards

The stack right now (if printed): \This program wasn't written in

Line 2:

Note that line 2 begins at the / because of the position of the pointer from line 1, and moves right to left.

v   "><>"r~/

/     deflects the pointer leftwards
~r    pops the / off the stack and then reverses it
"><>" pushes ><> onto the stack
v     deflects the pointer downwards

The stack right now: ><> ni nettirw t'nsaw margorp sihT

Line 3:

Like the previous line, this one begins at the \, which is where line 2 sends the pointer. Note that because the pointer wraps around the line when it reaches the first a I'll be writing my explanation in order of where the pointer goes (and thus what is executed)

a=", it was built for "+v \a

\aa=       deflect and push 1 onto the stack
", i ... " push the string onto the stack
+v         sum the last two values pushed and deflect

The stack right now(x is the character formed by the addition of "r" and a space. -- it is not the actual character, just a placeholder from me):

xof tliub saw ti ,><> ni nettirw t'nsaw margorp sihT

Line 4:

The pointer simply continues downwards so this line warrants no further explanation.

Line 5:

Starting at / and going leftwards.

.4b;!?lor"!nohtyP r"~/

~"r Python!" pops x off and adds back r and a space
r            reverses the stack
o            pops and prints a character
l?!;         pushes the length of the stack and stops if it's 0
b4.          pushes 11 then 4 then moves to that location (where o is)

The stack right now (the output reversed):

!nohtyP rof tliub saw ti ,><> ni nettirw t'nsaw margorp sihT

And that should be it for the explanation. Let me know if there is any inconsistency between the explanation/code or if I did anything wrong; I golfed down my code some more while I was in the middle of writing the explanation so I might have mixed bits of old and new code up.

  • It would be wrong if I said that you had to add the 3 because it isn't python 2. It's fine. – Blue Sep 3 '15 at 23:04
  • If this were Python only, I believe you could replace aa[:-2][::-1] with aa[-3::-1]. In this case, of course, that may interfere with the ><> formatting, but maybe it's worth considering if you haven't already. In particular I'm pretty sure you need a space below the v in the previous line, but it looks like print(aa[-3::-1]+"Pytho" would fit into the 24 characters below a=", it was built for "+, and then you could put a space followed by +"n"+a). I'm not sure if this would break something else, but if it works it will save a few bytes. – mathmandan Sep 4 '15 at 5:44
  • @mathmandan Great idea, I'll update my code and credit you. – cole Sep 4 '15 at 6:05
  • Nice job! FYI, I think this would work just fine in Python 2, and in Python 2 you can save a byte by dropping parentheses in the print statement: print aa[-3::-1]+"Pytho" +"n"+a. One other question though: in the ><> version, what happens to the 1 that was originally pushed onto the stack? – mathmandan Sep 4 '15 at 17:06
  • @mathmandan I think either the interpreter I used doesn't print the character if it's invalid or the printed character doesn't show at all. I'm honestly not 100% sure why printing that character doesn't matter; I discovered it accidentally. – cole Sep 4 '15 at 17:26

Javascript / C, 148 146 143 chars

//\
alert/*
main(){puts/**/("This program wasn't written in "//\
+"Javascript"+/*
"C"/**/", it was built for "//\
+"C!")/*
"Javascript!");}/**/

C: http://codepad.org/u8UimGLc http://codepad.org/Y80M5jpc http://codepad.org/m4DB2Ndd
Javascript: just copy code to browser console

  • 1
    @ Qwertiy, Brilliant! – user43512 Sep 3 '15 at 19:50
  • 2
    Beautiful.­­­­­ – Derek 朕會功夫 Sep 12 '15 at 23:26

CJam/GolfScript, 81 78 bytes

"This program wasn't written in "o"GolfScript"", it was built for ""CJam"oo"!"

Original 81 byte version:

"This program wasn't written in "["CJam"", it was built for ""GolfScript"]-1a%"!"

PHP/Perl, 98 96 bytes

$a="HP";$b="erl";
//;$a=$b;$b=HP;
print"This code wasn't written in P$a, it was built for P$b!";

Dunno if this is cheating or not, since as far as I can tell the only way to run PHP without an opening <? tag is something like php -r $(cat codefile.php). But assuming that's legal... // is a PHP comment, but in Perl it's a regex (which, in a statement by itself, doesn't do anything). The rest should be pretty self-explanatory.

Edit: Now using a bareword in the Perl-only part. I wanted to use those in the first place for both languages, but PHP displays a warning when you do that, contrary to "There should be no output to stderr."

  • 1
    <?'>#'; is valid syntax in both languages. – primo Apr 11 '17 at 5:53

Ruby/Python, 105 chars

a=["Ruby","Python"];a.sort();print("This program wasn't written in "+a[0]+", it was built for "+a[1]+"!")
  • This is really good! I especially like that there are no comments used. – styfle Sep 25 '16 at 3:31

Batch .BAT File / Batch .CMD File, 194 185 Bytes

@ECHO OFF
SET a=BAT
SET b=CMD
CALL :F&&GOTO :C||GOTO :O
:C
SET a=CMD
SET b=BAT
:O
ECHO This program wasn't written for %a% File, it was built for %b% File!
GOTO :EOF
:F
md;2>nul
SET v=1

Edit: Saved 9 bytes, and corrected a missing ! thanks to DLosc

Yeah, there's differences between BAT and CMD files. Reference. Essentially, CMD sets the ERRORLEVEL on a SET command, while BAT doesn't, meaning that here the ERRORLEVEL set by the malformed md command gets cleared by the SET v=1 in one version but not the other. This script is based on the example provided by "Ritchie" in that newsgroup thread.

Note that the shortened script above presumes ENABLEEXTENSIONS to be set ON (it is by default on every platform). The expanded script below explicitly sets it, to guarantee correct functionality. Without that, the SET command for CMD doesn't allow all extensions, and (on some systems, maybe) might not set the ERRORLEVEL appropriately.

Expanded and remarked

@ECHO OFF
setlocal ENABLEEXTENSIONS

REM Call the :FUNC subroutine and branch based on the resulting errorlevel
CALL :FUNC&&GOTO :CMD||GOTO :BAT

REM Just in case. If we reach this, though, hoo-boy ...
GOTO :EOF

:BAT
REM We're a BAT file, so set variables and goto output
SET a=BAT
SET b=CMD
GOTO :OUTPUT

:CMD
REM We're a CMD file, so set variables and goto output
SET a=CMD
SET b=BAT
GOTO :OUTPUT

:OUTPUT
REM Print out the result, then go to end of file
ECHO This program wasn't written for %a% File, it was built for %b% File!
GOTO :EOF

:FUNC
REM Simple subroutine to set the ERRORLEVEL appropriately
md;2>nul
REM Right now, ERRORLEVEL on both CMD and BAT is 1
SET v=1
REM Right now, ERRORLEVEL on CMD is 0, but BAT is still 1
  • ...there's a difference between CMD and BAT? – Stan Strum Apr 20 at 7:37

SWI-Prolog 6/SWI-Prolog 7, 156 bytes

P='SWI-Prolog ',A=6,B=7,(is_list(""),N=A,M=B;N=B,M=A),atomic_list_concat(['This program wasn\'t written in ',P,N,', it was built for ',P,M,'!'],W),write(W).

Uses the fact that double-quotes "" are string codes (i.e. list of character codes) in SWI-Prolog versions older than 7, and are a proper String type in version 7. is_list("") will thus be false in version 7 and true in earlier versions.

JavaScript 1.8/JavaScript 1.7, 90 bytes

a=!![].reduce;`This program wasn't written in JS 1.${7+a}, it was built for JS 1.${8-a}!`

Because Array.prototype.reduce is new in 1.8

EDIT: Golfed out 7 bytes by directly initializing a instead of using reverse()

EDIT: JavaScript can be written as JS, saving 8 bytes

EDIT: Thanks Hedi for pointing out that I can save 3 more bytes if I don't use the variable b any more

EDIT: Golfed out 6 bytes by computing 7+a and 8-a, where a=1 if reduce is defined (JS 1.8) and a=0 if it is not defined (JS 1.7)

EDIT: Hedi golfed out 6 more bytes suggesting the use of template string

EDIT: ETHproductions golfed out 2 bytes suggesting a=!![].reduce; instead of a=[].reduce?1:0;

  • With "JS" instead of "JavaScript", using twice " JS 1." in your string is shorter than using the variable b. – Hedi Sep 1 '16 at 17:52
  • You can use template string to make it shorter : `This program wasn't written in JS 1.${7+a}, it was built for JS 1.${8+a}!` – Hedi Sep 1 '16 at 23:38
  • 2
    I think you can save two bytes by changing a=[].reduce?1:0; to a=!![].reduce;. – ETHproductions Nov 3 '16 at 18:04
  • @ETHproductions Thanks, but I don't think it'd work. I need a to hold the value 1 or 0, not true or false – Mario Trucco Nov 4 '16 at 7:46
  • @ETHproductions sorry, you're right, I tried and it works. Thanks! – Mario Trucco Nov 4 '16 at 7:48

Ruby 1.8/Ruby 1.9, 87

puts"This program wasn't written in Ruby 1.#{?9%49}, it was built for Ruby 1.#{?8%47}!"

In Ruby 1.8, ?9 is the ASCII value of "9", which is 8 modulo 49. In Ruby 1.9, it's the string "9", and %49 is a formatting operation that does nothing since "9" doesn't have any format strings in it.

Python 2.7.9/Python 2.7.10, 127 bytes

We've had a couple posts that used minor versions, but none that have gone to the next level down...

import types
n=len(dir(types))
print"This program wasn't written in Python 2.7.%d, it was made for Python 2.7.%d!"%(n%33,-n%52)

Try it on Ideone (Python 2.7.10) and repl.it (technically Python 2.7.2, but should give the same result as 2.7.9).

Python 2.7.10, according to the changelog:

Added an __all__ to the types module.

This pushed len(dir(types)) from 42 to 43, giving a numerical difference we can exploit to generate the desired output.

Python/QBasic, 160 142 bytes

Tested with Python 3 and QBasic 1.1. Won't work in Python 2 without adding from __future__ import print_function to line 4.

1# DEFSTR A-B
a = "QBasic"
b = "Python"
'';a,b=b,a;PRINT=print
PRINT ("This program wasn't written in " + a + ", it was built for " + b + "!")
  • In Python, 1# is the expression 1 (no-op) followed by a comment. In QBasic, it's a line number (with the type suffix marking it as a DOUBLE). The DEFSTR statement tells QBasic that all variables whose names start with A or B (case-insensitive) are string variables. That way, we can call our variables a and b instead of a$ and b$ (which wouldn't work in Python).
  • In QBasic, ' begins a comment. In Python, '' is the empty string (no-op). Then we swap the language names and define an alias for the print function (since QBasic keywords are auto-formatted to uppercase).
  • The parentheses on the final line aren't necessary in QBasic, but don't hurt anything either.

If I'm allowed to turn off the autoformatter (which is an option in QB64, though not in the original QBasic), I can get it down to 114 bytes using Python 2:

1#DEFSTR A-B
a="QBasic"
b="Python"
'';a,b=b,a
print"This program wasn't written in "+a+", it was built for "+b+"!"

Perl/Ruby, 129 bytes

0&&eval('def sort a,b;[b,a] end');printf"This program wasn't written in %s, it was built for %s!",(@a=sort"Perl","Ruby")[0],@a[1]

No regular expression abuse in this one, just making the most of the fact that 0 is truthy in Ruby to eval a definition for sort (which actually reverses) and printfing. Ruby didn't like using the list for the arguments, so I had to do each one individually.

  • Why does a have to be an instance variable, as opposed to a local one? – Nic Hartley Sep 4 '15 at 1:30
  • @QPaysTaxes the @ sigil on a variable in Perl denotes that it's a list, in Perl storing in, say $a instead, yields no output. – Dom Hastings Sep 4 '15 at 5:00
  • Ah, I see. Thanks for the explanation. – Nic Hartley Sep 4 '15 at 13:18

Python / Retina, 133 120 119 117 115 bytes

Now that I know more about Retina and regexes, I've golfed it a bit more. It also actually works now.

#?.*
print"This program wasn't written in Python, it was built for Retina!"
#?.*t"

#?(\w+)(,.* )(.+)!"
#$3$2$1!
#

Python just prints the statement. Retina replaces anything with the Python print statement, then removes the print and any quotes. Then, I swap Python and Retina and remove the #.

Try in Python | Try in Retina

BF/SPL, 5342 bytes

I'm pretty sure this is the first Shakespeare Programming Language polyglot on this site.

Probably not going to win any prizes. Works by sneaking BF code into act/scene/program titles. The SPL code uses exclamation points instead of periods except for a few cases. The programs aren't supposed to take input, so the commas in the character declarations are "commented out" by zeroing cells and putting square brackets around the commas. The same procedure applies when hiding the square brackets around the enter/exeunt statements.

[-][.
Ford,.
Page,.
Act I:]+++++++++[>+++++++++<-]>+++.
Scene I:>[.
[Enter Ford and Page]
Ford:
You is sum of bad bad bad bad bad bad day and sum of bad bad bad bad day and bad bad day!Speak thy mind!
Scene II:]<<++[>++++++++++<-]>.
Page:
You is sum of bad bad bad bad bad bad day and sum of bad bad bad bad bad day and bad bad bad day!Speak thy mind!
Scene III:+.
Page:
You is sum of thyself and day!Speak thy mind!
Scene IV:++++++++++.
Page:
You is sum of thyself and sum of bad bad bad day and bad day!Speak thy mind!
Scene V:>++++[>++++++++<-]>.
Ford:
You is fat fat fat fat fat cat!Speak thy mind!
Scene VI:[-<+>]<<---.
Page:
You is sum of thyself and sum of big pig and pig!Speak thy mind!
Scene VII:++.
Page:
You is sum of thyself and fat cat!Speak thy mind!
Scene VIII:---.
Page:
You is sum of thyself and sum of big pig and pig!Speak thy mind!
Scene IX:--------.
Page:
You is sum of thyself and big big big pig!Speak thy mind!
Scene X:+++++++++++.
Page:
You is sum of thyself and sum of fat fat fat cat and sum of fat cat and cat!Speak thy mind!
Scene XI:<++++[->----<]>-.
Page:
You is sum of thyself and sum of big big big big pig and pig!Speak thy mind!
Scene XII:++++++++++++.
Page:
You is sum of thyself and sum of fat fat fat fat cat and big big pig!Speak thy mind!
Scene XIII:>.
Ford:
Speak thy mind!
Scene XIV:<++++++++++.
Page:
You is sum of thyself and sum of fat fat fat cat and fat cat!Speak thy mind!
Scene XV:<++++[->-----<]>--.
Page:
You is sum of thyself and sum of big big big big big pig and sum of fat fat fat cat and fat cat!Speak thy mind!
Scene XVI:<++++[>++++<-]>++.
Page:
You is sum of thyself and sum of fat fat fat fat cat and fat cat!Speak thy mind!
Scene XVII:-----.
Page:
You is sum of thyself and sum of big big pig and pig!Speak thy mind!
Scene XVIII:>+++++++.
Ford:
You is sum of thyself and sum of fat fat fat cat and pig!Speak thy mind!
Scene XIX:<++++++.
Page:
You is sum of thyself and sum of fat fat cat and fat cat!Speak thy mind!
Scene XX:>-------.
Ford:
You is sum of thyself and sum of big big big pig and cat!Speak thy mind!
Scene XXI:<+++.
Page:
You is sum of thyself and sum of fat cat and cat!Speak thy mind!
Scene XXII:-----.
Page:
You is sum of thyself and sum of big big pig and pig!Speak thy mind!
Scene XXIII:---------.
Page:
You is sum of thyself and sum of big big big pig and pig!Speak thy mind!
Scene XXIV:+++++++++++.
Page:
You is sum of thyself and sum of cat and sum of fat cat and fat fat fat cat.Speak thy mind!Speak thy mind!
Scene XXV:<+++[>-----<-]>.
Page:
You is sum of thyself and sum of big big big big pig and cat!Speak thy mind!
Scene XXVI:+++++++++.
Page:
You is sum of thyself and sum of fat fat fat cat and cat!Speak thy mind!
Scene XXVII:>.
Ford:
Speak thy mind!
Scene XXVIII:<-----.
Page:
You is sum of thyself and sum of big big pig and pig!Speak thy mind!
Scene XXIX:+++++.
Page:
You is sum of thyself and sum of fat fat cat and cat!Speak thy mind!
Scene XXX:>.
Ford:
Speak thy mind!
Scene XXXI:[->++<]>++.
Page:
You is sum of thyself and sum of big big big big big pig and sum of fat fat cat and cat!Speak thy mind!You is sum of thyself and sum of big pig and pig!Speak thy mind!
Scene XXXII:++++.
Page:
You is sum of thyself and big red hog!Speak thy mind!
Scene XXXIII:<+++++[>-----<-]>-.
Page:
You is sum of thyself and big big big big big pig!Speak thy mind!
Scene XXXIV:[-<+>]<------------.
Ford:
Speak thy mind!
Scene XXXV:<-----.
Page:
You is sum of thyself and sum of fat fat fat fat fat fat cat and sum of big pig and pig!Speak thy mind!
Scene XXXVI:+++++++++++.
Page:
You is sum of thyself and sum of fat fat fat cat and sum of fat cat and cat!Speak thy mind!
Scene XXXVII:>.
Ford:
Speak thy mind!
Scene XXXVIII:<+++.
Page:
You is sum of thyself and sum of fat cat and cat!Speak thy mind!
Scene XXXIX:<++++[->-----<]>--.
Page:
You is sum of thyself and sum of big big big big big pig and sum of fat fat fat cat and fat cat!Speak thy mind!
Scene XL:<++++[>++++<-]>++.
Page:
You is sum of thyself and sum of fat fat fat fat cat and fat cat!Speak thy mind!
Scene XLI:>.
Ford:
Speak thy mind!
Scene XLII:<<++++[>----<-]>-.
Page:
You is sum of thyself and sum of big big big big pig and pig!Speak thy mind!
Scene XLIII:<+++++[>++++<-]>-.
Page:
You is sum of thyself and sum of fat fat fat fat cat and sum of fat cat and cat!Speak thy mind!
Scene XLIV:------------.
Page:
You is sum of thyself and sum of big big big big pig and fat fat cat!Speak thy mind!
Scene XLV:+++.
Page:
You is sum of thyself and sum of fat cat and cat!Speak thy mind!
Scene XLVI:++++++++.
Page:
You is sum of thyself and fat fat fat cat!Speak thy mind!
Scene XLVII:>.
Ford:
Speak thy mind!
Scene XLVIII:<--------------.
Page:
You is sum of thyself and sum of big big big big pig and fat cat!Speak thy mind!
Scene XLIX:+++++++++.
Page:
You is sum of thyself and sum of fat fat fat cat and cat!Speak thy mind!
Scene L:+++.
Page:
You is sum of thyself and sum of fat cat and cat!Speak thy mind!
Scene LI:>.
Ford:
Speak thy mind!
Scene LII:>+++++++[<+++++++>-]<++.
Page:
You is sum of thyself and sum of big big big big big pig and big big big big pig!Speak thy mind!
Scene LIII:---.
Page:
You is sum of thyself and fat fat cat!Speak thy mind!
Scene LIV:----.
Ford:
You is sum of thyself and cat!Speak thy mind!
Scene LV:>+++++++[<------>-]<-.
Ford:
You is cat!
Scene LVI:>[.
[Exeunt]

Test out BF at https://repl.it/E8Hh/23.

SPL code was tested at the compiler found here: https://github.com/drsam94/Spl/.

/// and Retina, 95 + 3 = 98 bytes

/
//

This program wasn't written in \/\/\/, it was built for Retina!
/?./....(.*)(R.*)!
$2$1///!

+3 bytes for the -s flag in Retina.

Explanation for ///

The first instruction is

/
//

removes all newlines from the rest of the code, resulting in

This program wasn't written in \/\/\/, it was built for Retina!/?./....(.*)(R.*)!$2$1///!

Everything up to the ! is just a literal and printed to STDOUT. The next instruction is

/?./....(.*)(R.*)!$2$1/

But the search string ?. cannot be found, so nothing happens. Then the remaining code is //! which is an incomplete instruction so the program terminates, having printed the correct string.

Explanation for Retina

/
//

This tells Retina to replace / with //. But the input is empty, so this doesn't match anything.

<empty>
This program wasn't written in \/\/\/, it was built for Retina!

This replaces the input with the string in the second line.

/?./....(.*)(R.*)!
$2$1///!

This matches the string \/\/\/, it was built for Retina! and replaces it with Retina, it was built for ///! to give the correct result.

  • You know, I think it would be trivial to do one between Retina and rs... ;) – kirbyfan64sos Sep 9 '15 at 21:03
  • @kirbyfan64sos Probably, but how short would it be? ;) – Martin Ender Sep 9 '15 at 21:14
  • Well, so far I've gotten around 85 bytes + 3 for -s, though saying what I did would spoil the fun! :D – kirbyfan64sos Sep 9 '15 at 21:23
  • 1
    @kirbyfan64sos Go ahead and post it, I don't think I'll have time to look into rs any time soon. – Martin Ender Sep 9 '15 at 21:27

sed / Hexagony 251 Bytes

/$/cThis program wasn't written in sed, it was built for Hexagony!
#...>32;p;r;o;g;r;\+/;a;w;23+;m;a<.;.>s;n;+39;t;+32\s/;n;e;;t;i;r;w;<. |.>+32;i;n;+32;H;e\ ;/4+;y;n;o;g;a;x;< i>4;+32;i;t;+32;\;/u;b;23+;s;a;w<h>;i;l;t;+32;f\;/;s;23+;r;o;< T>e;d;+33;@

sed: Try it Online!
Hexagony: Try it Online!


In sed, it prints the correct string if it matches the empty string at the end (always). The second line is a comment. This does require a string on STDIN, but it can be empty (allowed based on this consensus).

Example:

echo '' | sed -f whatLanguage.sed

In Hexagony, the first / redirects to the bottom left, it follows the left side up to where the sed part starts, then just wraps left to right, down a line, right to left, down a line, and so on. The expanded hex looks like this:

         / $ / c T h i s p r 
        o g r a m w a s n ' t 
       w r i t t e n i n s e d 
      , i t w a s b u i l t f o 
     r H e x a g o n y ! # . . . 
    > 3 2 ; p ; r ; o ; g ; r ; \
   + / ; a ; w ; 2 3 + ; m ; a < .
  ; . > s ; n ; + 3 9 ; t ; + 3 2 \
 s / ; n ; e ; ; t ; i ; r ; w ; < . 
| . > + 3 2 ; i ; n ; + 3 2 ; H ; e \ 
 ; / 4 + ; y ; n ; o ; g ; a ; x ; < 
  i > 4 ; + 3 2 ; i ; t ; + 3 2 ; \
   ; / u ; b ; 2 3 + ; s ; a ; w <
    h > ; i ; l ; t ; + 3 2 ; f \
     ; / ; s ; 2 3 + ; r ; o ; < 
      T > e ; d ; @ . . . . . .
       . . . . . . . . . . . .
        . . . . . . . . . . .
         . . . . . . . . . .
  • I will never understand Hexagony... – DJgamer98 Oct 27 '16 at 13:35
  • @DJgamer98 I don't really understand it either. This is my first time using it. – Riley Oct 27 '16 at 13:44
  • You can shorten the Hexagony a bit by making use of the fact that ; takes the current cell mod 256 to determine a byte value (e.g. you can print a space with P0; regardless of the current cell value). This CJam script generates all the pairs: cjam.tryitonline.net/… – Martin Ender Oct 28 '16 at 19:58

JavaScript/CoffeeScript, 125 124 bytes

console.log("This program wasn't written in",(a=['Coffee','Java'])[+(b=0=='0')]+"Script, it was built for",a[b^1]+"Script!")

In CoffeeScript, a==b is compiled down to a===b, which makes the intermediate condition false. I used a bit of magic to convert the boolean value to an integer.

Saved 1 byte thanks to @DomHastings!

125-byte version:

console.log("This program wasn't written in",(a=['Coffee','Java'])[(b=0=='0')+0]+"Script, it was built for",a[b^1]+"Script!")
  • Nice! I think you can save a byte with +(b=0=='0') instead of +0! – Dom Hastings Sep 3 '15 at 15:55
  • Instead of b^1, I think you can use ~b – Ismael Miguel Sep 9 '15 at 22:25
  • @IsmaelMiguel Nope. It says it was built for undefinedScript. – kirbyfan64sos Sep 10 '15 at 17:22
  • I forgot that ~1 == -2. But (b=0=='0')+0 can be written as +(b=0=='0'). Or b=+(0=='0'). That should cut off 1 byte. – Ismael Miguel Sep 10 '15 at 17:47
  • @IsmaelMiguel I already did that in the most recent version... – kirbyfan64sos Sep 10 '15 at 18:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.