Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The determined Real Programmer can write Fortran programs in any language.

from Real Programmers Don't Use Pascal

Your task is to write program in your programming language of choice, but you are allowed to use only another language. That is, throw away all coding conventions from one language and replace them with coding conventions from other language. The more the better. Make your program look as if it was written in another language.

For example, Python fan who hates Java could write following Python program in Java:

void my_function()                                                             {
    int i = 9                                                                  ;
    while(i>0)                                                                 {
        System.out.println("Hello!")                                           ;
        i = i - 1                                                              ;}}

Pascal enthusiast forced to use C could write this:

#define begin {
#define end }
#define then
#define writeln(str) puts(str)

if (i == 10) then
begin
    writeln("I hate C");
end

You have to write complete program. The program desn't have to do anything useful.

Good Luck. This is a popularity contest so the code with the most votes wins!

share|improve this question
1  
@m.buettner create your file with the extension .litcoffee. It might help. –  Ismael Miguel Mar 20 '14 at 22:53
    
A little long (and previously-written; and not self-contained) for an answer, but: Postscript scanner in Postscript in C. –  luser droog Mar 21 '14 at 4:36
39  
I don't think you (or the majority of the answers) understand the point of the quote. It's not that a Real Programmer writes code that looks lexically like Fortran even though he's writing in Pascal or LISP: it's that he applies a Fortran way of thinking even when writing in Pascal or LISP; e.g. "As all Real Programmers know, the only useful data structure is the Array.". Great answers would be procedural code in Prolog, functional code in C, object-oriented code in Pascal. –  Peter Taylor Mar 21 '14 at 11:10
1  
I hope someone's gonna do a Lisp dialect in, well, anything but another Lisp dialect... –  itsjeyd Mar 21 '14 at 21:23
5  
@itsjeyd Greenspun's Tenth Rule Of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of CommonLisp." –  Joshua Taylor Mar 25 '14 at 17:38

38 Answers 38

C# in Whitespace

Okay, first try at one of these, so let's see how it goes.

using System; //very important  

namespace ConsoleApplication1  //namespace: name whatever you want      
{ 
 //start    
 class  Program  //class name:  also anything    
    {
    //main function 
    static void Main(string[] args) {
        for(int i=0;i<10;i++)   writeOutput(i); 
    } //end main    
    static void writeOutput(int i) { Console.WriteLine(i); }    //display output    


    } //class ends here         

}  //close namespace:   also very important     





//yay!

And in case the formatting went screwy from having to put four spaces at the front of each line, here it is again with . for space and # for tab:

using.System;.//very.important#

namespace.ConsoleApplication1..//namespace:#name.whatever.you.want##
{.
.//start#
.class#Program..//class.name:#also.anything#.
#{
....//main.function#
#static.void.Main(string[].args).{
....#for(int.i=0;i<10;i++)#writeOutput(i);#
#}.//end.main#
#static.void.writeOutput(int#i).{.Console.WriteLine(i);.}#//display.output#

.
.#}.//class.ends.here.##

}..//close.namespace:#also.very.important#.#
.




//yay!
share|improve this answer

Brainfuck in JS

[][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[
!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[
+!+[]]]]][([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(
![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!
![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[]]]]+([][(![]+[])[+[[+[]]]]+([][[]]+
[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]
]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[
]+!+[]+!+[]+!+[]]]]+([][[]]+[])[+[[+!+[]]]]+(![]+[])[+[[!+[]+!+[]+!+[]]]]+(!!
[]+[])[+[[+[]]]]+(!![]+[])[+[[+!+[]]]]+([][[]]+[])[+[[+[]]]]+([][(![]+[])[+[[
+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!
![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+
[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[
[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!!
[]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[]+!+[]
+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]((![]+[])[+[[+!+[]]]]+(![]+[])[+[[!+[]+!+
[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]+(!![]+[])[+[[+[]]]
]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[
+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[
+[[+!+[]]]]]+[])[+[[+!+[]]]+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+[+!+[]]+([][(![]+[]
)[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]
]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+
[])[+[[+!+[]]]+[[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]]])()
share|improve this answer
10  
I don't see any brainfuck here. Not even a single char in ><,.- –  Mig Mar 21 '14 at 13:11
8  
@Michael: Who said it isn't a program that makes an infinite loop? –  xfix Mar 21 '14 at 16:12
14  
is this JSF*ck? –  professorfish Mar 22 '14 at 19:05
8  
How on Earth does it do that? –  nandhp Mar 26 '14 at 15:57
2  
Oo. Somebody finally did this. I spent some time trying to figure out how to write a JS program using only the characters +![]() but never could quite figure it out. I need to analyze this when I have time... –  Matti Virkkunen Mar 31 '14 at 12:23

C in C++

#include <stdio.h>

int main(int argc, char** argv)
{
        printf("Hello world!\n");
        return 0;
}
share|improve this answer
54  
I see what you did there;) –  el.pescado Mar 21 '14 at 10:38
21  
Well, that's a cheap trick, seeing as C++ is 'backwards compatible' with C. –  Agi Hammerthief Mar 21 '14 at 23:29
5  
@AlexM. I think it would be more in the spirit of the question if this was a longer (procedural) example which would clearly benefit from using some classes and which uses other C idioms where some STL goodness would be much more reasonable (say char* instead of std::string). –  Martin Büttner Mar 22 '14 at 10:48
41  
Valid in C, C++, Objective-C, and Objective-C++! What a wonderfully polyglot answer. –  nneonneo Mar 23 '14 at 0:23
5  
@BenJackson Psh, real C programmers use char *argv[]! –  Thomas Mar 26 '14 at 2:14

SML in Java

I still have some ancient code around from when I started learning Java and tried to use it in a functional style. Slightly cleaned up:

/**
 * Genericised ML-style list.
 */
public class FunctionalList<T> 
{
    private final T head;
    private final FunctionalList<T> tail;

    public FunctionalList(T x, FunctionalList<T> xs) {
        this.head = x;
        this.tail = xs;
    }

    public static <T> FunctionalList<T> cons(T x, FunctionalList<T> xs) {
        return new FunctionalList<T>(x, xs);
    }

    public static <T> T hd(FunctionalList<T> l) {
        return l.head;
    }

    public static <T> FunctionalList<T> tl(FunctionalList<T> l) {
        return l.tail;
    }

    public static int length(FunctionalList<?> l) {
        return len(l, 0);
    }

    private static int len(FunctionalList<?> l, int n) {
        return l == null ? n : len(tl(l), n + 1);
    }

    public static <T> FunctionalList<T> rev(FunctionalList<T> l) {
        return rev(l, null);
    }

    private static <T> FunctionalList<T> rev(FunctionalList<T> a, FunctionalList<T> b) {
        return a == null ? b : rev(tl(a), cons(hd(a), b));
    }

    public static <T> FunctionalList<T> append(FunctionalList<T> a, FunctionalList<T> b) {
        return a == null ? b : cons(hd(a), append(tl(a), b));
    }
}
share|improve this answer

PHP and Javascript

This is a polyglot:

You can run this code in both languages:

if("\0"=='\0')
{
    function printf(){
        $b=Array();
        $a=$b['slice']['call'](arguments);
        $a=$a['join']('');
        console.log($a);
        return $a.length;
    };

    function strtoupper($s){return $s['toUpperCase']();}

    function count($a){return $a['length'];}
}

printf('this is cool!');

$c=Array('a','b','c','d');

for($i=0,$l=count($c);$i<$l;++$i)printf("\n",strtoupper($c[$i]));

The trick here is that Javascript uses escape sequences in strings starting with ' and ".
On the other hand, PHP only uses escape sequences in strings starting with " and <<<.

Then, we declare the function printf, which is similar to print but outputs a formated string in PHP.

PHP requires that vars start with $, while Javascript simply allows.

share|improve this answer
    
Nobody's using Array(…) in JS, and it's clearly array(…) in PHP. […] would be far better ;) ! –  Blackhole Mar 23 '14 at 0:49
7  
I don't care if people use Array() in JS or not: I care that I have a TRUE polyglot. I'm making one of the worst JS crimes with this code but all I want is that it runs and does the exact same thing in both, but looking like JS and PHP at the same time. –  Ismael Miguel Mar 23 '14 at 18:29
    
And btw, [...] is invalid in PHP <5.4.0, which is bad....... If I throw this into PHP 4, 5 or Javascript, I expect it to work, instead of giving syntax errors everywhere. –  Ismael Miguel Mar 23 '14 at 20:31
1  
If you want your code to look like JS, you must use […], which seems pretty standard in PHP, and therefore is OK for your goal. And by the way, PHP < 5.4? Time to update, guy… –  Blackhole Mar 23 '14 at 20:48
8  
Compatibility is more important than "looks". And Array is the RIGHT name of the Array object's constructor. Basicly, using [] is the same as Array(). I don't see anything bad with it. But I have one simple question: Works? (btw, I have to use php 5.3.28 at work.) –  Ismael Miguel Mar 23 '14 at 21:29

JavaScript in C

I like JavaScript, and I like C. Surely mixing the two would create the best language in the world!!11elevenone

#include <stdio.h>
#define function int
#define var int
struct { int (*log)(const char *,...); } console = { printf };

/* From here on only JavaScript! */

function fac(x){
    if(x < 2) return 1;
    return x * fac(x - 1);
}

function main(){
    console.log("Hello world!\n");

    for(var i = 0; i < 10; i++){
        console.log("%i! = %i\n", i, fac(i));
    }

    return 0;
}

// *Should* we export the main function of this library??/
exports.main = main;

Nope, that's revolting. Ah well... Using a format-string for console.log isn't very JavaScript-y, but it does work (at least in Chrome's developer console).

share|improve this answer
3  
+1 ... Except if this was proper JavaScript you would have to call that main somehow ;) –  Martin Büttner Mar 20 '14 at 20:26
4  
@m.buettner True, added a dirty hack to pretend we're exporting the main function, like one would do in Node.js libraries. –  Kninnug Mar 20 '14 at 20:35
3  
Well, the actual trick is the trigraph. You might as well just do main(); in that last line, so you don't need any Node.js assumption. ;) Anyway, I'd +1 again if I could. –  Martin Büttner Mar 20 '14 at 20:40
2  
+1 for the "!!elevenone" joke... –  itsjeyd Mar 21 '14 at 21:16

F# in C++

Rather unimaginative and nasty abuse of the preprocessor. I thought it'd be fun to alter C++ to look like a completely dissimilar language instead of using a few aliases to make it look like Java or PHP. I'm not really expecting this to garner a ton of upvotes, it's a just-for-fun entry.

#define let int
#define args ( int __, char* args[] ) { int ___ 
#define println printf(
#define exit "\n" ); return 0; }
#include <stdio.h>

let main args =
    println "F# is better than C++"
    exit

Try it here.

Sadly writing something to STDOUT is about all it can do, although I'm sure if someone threw enough witchcraft at it they could make it do more.

share|improve this answer
1  
For the last line to work in F#, it would either have to be exit 0 or just 0. –  Jwosty Mar 27 '14 at 3:44

JavaScript in Ruby

Number.prototype.times = function(f){
  for (var i=this; --i;) f()
}
end = 0
function do(s) {return new Function(s)}

//;# Ruby starts here:
(5).times(do("alert('hi!'); end"))
share|improve this answer
7  
Isn't this ruby in javascript? Not the other way around? –  shelvacu Mar 21 '14 at 6:02

protected by Community Mar 28 '14 at 15:45

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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