raganwald
(This is a snapshot of my old weblog. New posts and selected republished essays can be found at raganwald.com.)

Tuesday, December 19, 2006
  Just because we can


The following program can be compiled in ten different programming languages—and it prints something different in all of them. The languages: C, C++, Perl, TeX, LaTeX, PostScript, sh, bash, zsh and Prolog:

    %:/*:if 0;"true" +s ||true<</;#|+q|*/include<stdio.h>/*\_/
{\if(%)}newpath/Times-Roman findfont 20 scalefont setfont(
%%)pop 72 72 moveto(Just another PostScript hacker,)show((
t)}. t:-write('Just another Prolog hacker,'),nl,halt. :-t.
:-initialization(t). end_of_file. %)pop pop showpage(-: */
int main(){return 0&printf("Just another C%s hacker,\n",1%
sizeof'2'*2+"++");}/*\fi}\csname @gobble\endcsname{\egroup
\let\LaTeX\TeX\ifx}\if00\documentclass{article}\begin{doc%
ument}\fi Just another \LaTeX\ hacker,\end{document}|if 0;
/(J.*)\$sh(.*)"/,print"$1Perl$2$/"if$_.=q # hack the lang!
/
sh=sh;test $BASH_VERSION &&sh=bash;test $POSIXLY_CORRECT&&
sh=sh;test $ZSH_VERSION && sh=zsh;awk 'BEGIN{x="%c[A%c[K"
printf(x,27,27)}';echo "Just another $sh hacker," #)pop%*/

Snarfed (with awe and respect) from pts oldalai.
 

Comments on “Just because we can:
Oooh, scary cool.

Jurassic Park Syndrome: Just because you can do something, doesn't mean you should.

This is a good example of the dichotomy of the good programmer mentality: you should never ever do things like this, but you should know how do things like this.

I get around this dichotomy by adding "at work" and "at play" to the don't and do phrases. :-)
 
How is writing a program that compiles in ten languages different from writing a big Data Abstraction Layer so that you can change your corporate database from Oracle to Sybase just in case you feel like firing your entire DBA team and hiring another?

Hmmm. Maybe it isn't different? Maybe you shouldn't do that either?
 
Well, both programs are dishonest. The first has sacrificed all other virtues to accomplish one task: being compilable in 10 different languages. It's unreadable and unmaintainable, but we want to be seen as clever and smart so we put it in the project anyway.

Your example IS different, but only in that the lie has moved to middle management. The cost of firing and hiring all new Oracle DBAs is far less than developing an abstraction to allow a competing techonolgy, migrating the system to that technology, and then firing and hiring all new DBAs for that system. But the manager really, really wants those DBAs out of his hair so he sinks 30X the resources into something that will ultimately add nothing to the viability or profitability of the company. This massive funding hemorrhage will be sold to upper management as a strong technology investment, but this is all a lie invented to keep the real agenda hidden.
 
It would be great to support more modern popular languages like Python, Ruby, Java, Javascript, PHP ...
 
You're the skillest hackah I've ever seen. Next step: malebolge, language from hell :) http://en.wikipedia.org/wiki/Malebolge
 
Reginald, if you had read correctly you would have figured out that its not one program that compiles in ten languages but one source file that will make 10 different programs based on the language used. so in reply to chnrxn, its not a matter of supporting popular languages like python or PHP your pushing each language to the extremes of its ability so where Java or ruby doesn't support the syntax for this "trick"
 
Reginald, if you had read correctly you would have figured out that its not one program that compiles in ten languages but one source file that will make 10 different programs based on the language used.

I agree whole-heartedly!

I'm sure you'll agree with me when I say that the only time the source file is the program is when the underlying language is homoiconic, for example a Snobol program.
 
This sophomoric exercise reminds me of the logic/math puzzles of NEW SCIENTIST magazine in the Enigma column. They are highly contrived and can be solved by mechanistic methods [eg a spreadsheet or quickie program], or a suite of related compilers/interpreters.

It's a harmless trick but does little to advance understanding, insight, or knowledge. Still ... one never knows where idleness can lead.
 
I don't get it. How would you turn that mess into a c or c++ program?
 
Ummm... That mess is a C and also a C++ program. Try saving it in a file and compiling it into an executable.
 




<< Home
Reg Braithwaite


Recent Writing
Homoiconic Technical Writing / raganwald.posterous.com

Books
What I‘ve Learned From Failure / Kestrels, Quirky Birds, and Hopeless Egocentricity

Share
rewrite_rails / andand / unfold.rb / string_to_proc.rb / dsl_and_let.rb / comprehension.rb / lazy_lists.rb

Beauty
IS-STRICTLY-EQUIVALENT-TO-A / Spaghetti-Western Coding / Golf is a good program spoiled / Programming conventions as signals / Not all functions should be object methods

The Not So Big Software Design / Writing programs for people to read / Why Why Functional Programming Matters Matters / But Y would I want to do a thing like this?

Work
The single most important thing you must do to improve your programming career / The Naïve Approach to Hiring People / No Disrespect / Take control of your interview / Three tips for getting a job through a recruiter / My favourite interview question

Management
Exception Handling in Software Development / What if powerful languages and idioms only work for small teams? / Bricks / Which theory fits the evidence? / Still failing, still learning / What I’ve learned from failure

Notation
The unary ampersand in Ruby / (1..100).inject(&:+) / The challenge of teaching yourself a programming language / The significance of the meta-circular interpreter / Block-Structured Javascript / Haskell, Ruby and Infinity / Closures and Higher-Order Functions

Opinion
Why Apple is more expensive than Amazon / Why we are the biggest obstacles to our own growth / Is software the documentation of business process mistakes? / We have lost control of the apparatus / What I’ve Learned From Sales I, II, III

Whimsey
The Narcissism of Small Code Differences / Billy Martin’s Technique for Managing his Manager / Three stories about The Tao / Programming Language Stories / Why You Need a Degree to Work For BigCo

History
06/04 / 07/04 / 08/04 / 09/04 / 10/04 / 11/04 / 12/04 / 01/05 / 02/05 / 03/05 / 04/05 / 06/05 / 07/05 / 08/05 / 09/05 / 10/05 / 11/05 / 01/06 / 02/06 / 03/06 / 04/06 / 05/06 / 06/06 / 07/06 / 08/06 / 09/06 / 10/06 / 11/06 / 12/06 / 01/07 / 02/07 / 03/07 / 04/07 / 05/07 / 06/07 / 07/07 / 08/07 / 09/07 / 10/07 / 11/07 / 12/07 / 01/08 / 02/08 / 03/08 / 04/08 / 05/08 / 06/08 / 07/08 /