Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

Quadrilingual Crazy Programming

Posted by timothy on Sat May 11, 2002 08:05 PM
from the esperonto-has-nothing-on-this dept.
mtve writes: "Have you ever seen source code that is valid on four languages: Perl, C, Befunge, and BrainF*ck? During last Perlgolf season famous Perl hacker Jérôme Quelin submit such inconceivable masterpiece and now he published expanded explanation of his solution. Caution: that text can hurt your mental health. Play Perlgolf!"
+ -
story
This discussion has been archived. No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More
Loading... please wait.
  • by PepsiProgrammer (545828) on Saturday May 11 2002, @08:07PM (#3504140)
    I have enough trouble making my code compile in ONE language, 4 at the same time is a bit much
  • omg... (Score:5, Funny)

    by Danse (1026) on Saturday May 11 2002, @08:12PM (#3504156)

    He should seek professional help. Soon. That's right up there with self-mutilation.

    • Re:omg... (Score:4, Funny)

      by SkulkCU (137480) on Saturday May 11 2002, @08:28PM (#3504215) Homepage Journal

      He should seek professional help. Soon.

      at the bottom of the page:

      the referees were so impressed by my efforts (one of them told me that I deserve a book. And a straightjacket. And a padded room [...] they decided to grant me a book for my efforts.
      • Re:omg... (Score:4, Insightful)

        by red_dragon (1761) on Saturday May 11 2002, @11:12PM (#3504569) Homepage
        Please, someone write a +5 insightful essay about why I shouldn't do this...before it's too late.

        I'm afraid that before someone writes such an essay, someone else will have written a +5 Funny post on why you *should* do it. Sorry.

  • Now this is exactly why I am proud to be a programmer. Screw Picaso and DaVinci, this is what real art is all about.

    This presents an interesting dilemma though. What Emacs mode do I use to look at the code??? perl-mode, c-mode, I dunno. Fontifying just complicates it even more.

    At any rate, this shit is going up in my cube. If they should this in a frame, I would buy it and hang it in my house.
      • No, it was partly a joke and partly serious.

        I think there is just as much beauty in code as in many works of art.

        Mozart is famous for his table top pieces. These are pieces where two muscians sat on opposite ends of a table with a piece of sheet music in the middle. The first piece played the piece right side up, and the second played the piece upside down. His genius was being able to create with such incredible restrictions.

        Solving a problem in multiple languages represents a similiar ability. Of course, it wasn't a terribly complicated problem, but the basic idea is the same.
  • by Lardmonster (302990) on Saturday May 11 2002, @08:14PM (#3504167)
    http://www-2.cs.cmu.edu/afs/cs/user/chogan/Web/pol yglot

    Cobol, Pascal, Fortran, C, Postscript, shellscript, 8086
    • No. No, no. These people should have been shot. THIS IS NOT NORMAL! Go find a shrink!

      On the other hand, it's really, really cool.. How about a contest? Write a program that does a particular thing (dunno, calculate something (like Pi)), and should be compilable/runnable in a lot of languages. You get points from a jury from how good the program solved the task, times number of languages it is written in. Could be quite fun. (-8
  • by Anonymous Coward on Saturday May 11 2002, @08:17PM (#3504184)
  • by Zifter (15237) on Saturday May 11 2002, @08:20PM (#3504194)
    What about the famous Polyglot [mcc.ac.uk]?

    It runs/compiles under 7 languages: ANSI COBOL, ISO Pascal, ANSI Fortran, ANSI C, PostScript, Shell Script, and 8086 machine language!!! Check it out, it rocks.

  • by Sancho (17056) on Saturday May 11 2002, @08:23PM (#3504202) Homepage
    He uses the fact that # is a comment in Perl VERY frequently to use #defines etc that will allow C to act like Perl. Interesting solution, although I question whether the use of such preprocessor directives REALLY counts as making cross-compatible code. Then again, I nitpick the difference between preprocessor and compiler, so...
  • Well, I guess that is one way to keep people from saying that your implementation isn't portable enough.
  • by damiam (409504) on Saturday May 11 2002, @08:24PM (#3504205)
    This program is valid in C, C++, python, perl, basic, and a few other languages, and it also accomplishes the rare feat of printing its own source code without reading from a file:





    Note that, even though this is standard C, gcc won't compile it, complaining about the lack of a "main" function.

  • It's neat, but... (Score:5, Insightful)

    by meta-monkey (321000) on Saturday May 11 2002, @08:26PM (#3504209)
    First off, this guy obviously has much better kung-fu than I do...I've never even heard of BrainFuck or Befunge...but I think he kinda cheated on the last two languages. He just hid the code for Brainfuck and Befunge in perl and C comments, so they wouldn't interfere with each other. Now, the perl/C part is really neat, because he used C #defines to translate various Perl characters into C, so the Perl interpreter and C compiler are reading and understanding the exact same code...that's cool. But the B & B code just gets ignored by the Perl interpreter and C compiler because of comments, so this amounts to writing 3 seperate programs (one in BrainFuck, one in Befunge, and one that's bi in C and Perl) and then putting them all in the same file with intstructions as to which compiler/interpreter reads which part, as opposed to writing one piece of code that's meaningful in all 4 languages. I'd call this bilingual, not quadlingual.
  • Too Bad... (Score:5, Funny)

    by susano_otter (123650) on Saturday May 11 2002, @08:28PM (#3504212) Homepage
    Have you ever seen source code that is valid on four languages: Perl, C, Befunge, and BrainF*ck? During last Perlgolf season famous Perl hacker Jérôme Quelin submit such inconceivable masterpiece and now he published expanded explanation of his solution. Caution: that text can hurt your mental health.

    Ironically, the article isn't even valid in one language.

  • Yeah, it's great to have a piece of code that compiles for four languages, but what's the point if you're just using pre-processor and compiler tricks to get the compiler to look at a different section of the same file? In this file, if I change the problem-solving logic slightly, I have to change it in several places. What would be truly cool (and incredibly difficult), in my opinion, would be to completely eliminate redundant logic.
    • In this file, if I change the problem-solving logic slightly, I have to change it in several places. What would be truly cool (and incredibly difficult), in my opinion, would be to completely eliminate redundant logic.

      What?!? Eliminate redundant logic? That would put microsoft out of business!
      MacOS ~ Windows
      Java ~ C#
      Diablo ~ Dungeon Siege
      • While funny, I gotta call you on that last one. If games with similar premises/engines were not out there, the market would be incredibly smaller. For example, we'd only have Doom.....Ok, so we allow upgrades. We'd have Q3. We'd never have gotten the beauty that is halflife :)
  • Befunge? (Score:4, Funny)

    by Quixote (154172) on Saturday May 11 2002, @08:32PM (#3504219) Homepage Journal
    From the article (yes, I read it, and yes, my jaw won't close now):
    Befunge is, ... a topological language on a 2D cartesian Lahey space.

    Holy mackerel! I am in love. I've found the object of my dreams.

  • by KFury (19522) on Saturday May 11 2002, @08:33PM (#3504225) Homepage
    ...on th web, anyhow. All the time we deal with several languages, burying one inside another so they'll make sense as they go through successive levels of parsing.

    For example, every day I write SQL that is buried in PHP libraries which extracts more PHP that in turn has HTML and Javascript in them.

    For another example of the crazyness, check this simple example [fury.com]. Now if you look at the source, you'll notice the end part of that A-tag was: .');"> For those of you who are counting, that's SIX 'enders' in three syntax languages just to form a simple alert box.

    . - English syntax
    ' - Javascript string syntax
    ) - Javascript function syntax
    ; - Javascript instruction syntax
    " - HTML attribute syntax
    > - XML (err, HTML, whichever) tag syntax

    And that's not even a particularly hairy example. That's just client-side and wetware-side parsing.
  • Wow, if I saw that a few years ago, I would've been so scared I would've never touched another programming language again! Slashdot, please think of the children before you post things like that.
  • When I get into my "programming language learning" mode, when I learn a new procedure in one language, I try to make the same program in at leats 7 others....sure, it sounds lame, but it really sticks in my head..
  • just curious (Score:5, Interesting)

    by Permission Denied (551645) on Saturday May 11 2002, @09:05PM (#3504283) Journal
    #define ARGV argv

    Why not just do this instead:

    main (int argc, char *ARGV[])

    Also, another minor quip: the C program is not valid C in either C89 or C99. It's not valid C89 because it uses '//' for a comment, and it's not valid C99 since it introduces main() without declaring the return type. C89 defaults to int if you don't declare the type (both for functions and variables, which can be fun), whereas this behaviour is undefined in C99. Normally, I don't follow the anal-retentive lingual purists, but I think this situation calls for this.

    But yeah, this is pretty cool.

    • Yeah, many people don't think about that. When I write C code I always write:

      main (int ac, char *av[])
      Some weird habit I picked up but it drives people nuts.. "hey, is that legal in C?", etc.
  • 5 languages...I'll bet they forgot to count C++
  • by theolein (316044) on Saturday May 11 2002, @09:35PM (#3504343) Journal
    I sort of think it's things like this that make slashdot so worth reading. These gems of pure geekness. +5 to /.

    I discovered BrainFuck by chance two years ago and immediately got lost in two nights of trying to get my first quine to compile in the interactive JavaScript BF interpreter. For some perverse reason it is fun. It brings out the little boy in me who used to build model airplanes out of toothpicks: Little unimportant things that become something when you stick them together. perhaps this would also be a possible real world language for programming Nanobots, whose processors wouldn't yet cope with a P4 strapped to their backs.
    • Can you seriously imagine on the Nightly News them doing a story about how BrainFuck programmed NanoBots kill AIDS cells, and kill off tumors?

      News Reporter: So how'd you do it?
      Guy: Well, the nanotech was already there. I just wrote a small program in BrainFuck in order to hunt down the virus.

      Well, at least censorship would go out the window the same time we cure World Disease.

  • by Clay Mitchell (43630) on Saturday May 11 2002, @10:13PM (#3504432) Homepage
    Try to get a website to look the same in every web browser.

    That's an impressive feat?

    Netscape 4.X = Worst browser ever!
    • Exactly! Just try writing cross-platform JavaScript! There seems to be little pattern to which browsers support window.innerWidth vs. which support document.body.clientWidth, and since the alternative browsers usually munge their USER_AGENT strings you can't rely on that to be meaningful. Sure, you can support Netscape and MSIE, but can you tell me which one of those properties will work and which one will abort with an error in Opera, iCab, OmniWeb and Konqueror? I wound up with this, which I think seems to work universally, mostly, kinda:

      if(!isNaN(parseInt(window.innerWidth))) {
      pageWidth=parseInt(window.innerWidth);
      } else {
      if(!isNaN(parseInt(document.body.clientWidth))) {
      pageWidth=parseInt(document.body.clientWidth);
      } else {
      pageWidth=700;
      }
      }


      Falling back on a default if neither works.

      Does someone have a better suggestion?
        • "We detect that you're using the Konqzilla browser. Since by using Konqzilla our site may appear 'fugly', we have no other alternative but to deny you entrance."

          "Your browser identifies itself as Internet Explorer 5.5 running on Windows XP. But it's really Konqzilla. I don't know why you want to see our site using Konqzilla. Please use a modern browser to view this site. Now go away."

          "It appears that you're running Internet Explorer 5.5 running on Windows XP. And it's the real thing this time. But you're running it under WineX. Do you think we're stupid. We don't want you to view our site. Now bugger off!"

          "Ah, a genuine Internet Explorer 5.5 running on a genuine Windows XP! Congratulations. But you have your monitor temperature set to 5000! Our site was designed for 9300. It will look fugly without it. Please correct the situation before returning"
  • My colleagues at work think *I* am nuts because I refuse to use notepad or Visual Café's builtin-text editor to edit .java source files, preferring instead a much more powerful older DOS-based text editor (TSE [semware.com]). They're gonna suffer a heart attack when they see this.
  • I don't know if there's an official term for it, but I often write a function in one language that writes code for another language - for example, using PHP or Perl to write javascript. I've even gone so far as to use PHP to write a SQL statement which in turn is used to determine what will go in a Javascript function that writes HTML code. The example below is rather pointless, but I have come across real situations where it is beneficial to use that many languages together.

    function hello() {
    $q = "select * from helloworld where id=1;";
    $connect = pg_pconnect("dbname=hw user=webber password=dipsh*t");
    $cursor = pg_exec($connect, $query);
    $r = pg_fetch_row($cur,0);
    echo "<script>\n";
    echo "function do_it() {\n\tdocument.write(\"".$r[0]."\");\n}\n";
    echo "</script>";
    }

    That's three "real" languages and one markup language. And if you think that was crazy, think about this - I just had to write that in HTML in a slashdot posting textbox!

    My next goal is to make the javascript write out HTML for parameters in a Java applet. And, the whole PHP page is going to be written by a C program exectuted by a cron job that was set up via a perl script (webmin!) That's eight different languages - perl -> bash (I think) -> C -> PHP -> SQL and Javascript -> HTML -> Java. Sadly, I don't think that's nearly as crazy as the quadrilingual program. I need to learn Brainf*ck.

  • by The Pim (140414) on Saturday May 11 2002, @11:58PM (#3504685)
    If you want to test your knowledge of esoteric programming languages, try this problem [mit.edu] from the 2002 MIT Mystery Hunt [mit.edu].

    I was happy to solve 1840, even though I immediately recognized the language, because it is poorly specified and there is no interpreter. But that was nothing compared with my teammates, who solved 1183 with nothing but the problem and pure reason.

  • Editors, edit! (Score:4, Insightful)

    by czth (454384) on Sunday May 12 2002, @12:40AM (#3504779) Homepage
    mtve writes: "Have you ever seen source code that is valid on four languages: Perl, C, Befunge, and BrainF*ck? During the last Perlgolf season famous Perl hacker Jérôme Quelin submitted one such inconceivable masterpiece and now he has published an expanded explanation of his solution. Caution: that text can hurt your mental health. Play Perlgolf!"

    Why is it that Slashdot's editors can't fix the mistakes in the above and many, many other articles before posting? Either they don't notice the errors (in which case they should be sacked and replaced), or it's that important to post the article a minute earlier (highly unlikely), or somehow the original wording is considered "sacred" and Not To Be Changed (stupid if true). Come on here. Does the error rate on the front page have to be so high?

    The only reason I'm not blaming the submitter (mtve) as well is because it's possible English is not his first language (or even his second). If it is, shame on him too. We all deserve better.

    (Oh yes, for those clueless enough to say "What errors", I threw in the required changes in bold. Also, isn't one Perlgolf link enough?)

    czth

  • This would be worth a subscription if there was more of it (not more of the same but more in a similar vein of geekiness). REAL geek news! None of that Kayz crap or friking freshmen case mods with melted plastic and neon bulbs. Let them come and stare in awe as the fail to grok the code because the wasted their lives drilling holes in their PC case.
  • by the_danielsan (512604) on Sunday May 12 2002, @03:05AM (#3505073)

    I wrote a Brainfuck interpreter in PHP [lorch.cc] a while ago which also includes a short introduction [lorch.cc] to the language.

    By the way, Brainfuck was initially named "Mental Masturbation", but the Author Oliver Müller then stuck to a less offensive name :)

    • Have you read the article?

      This is an excerpt of the Befunge part:
      > 2ep 1ep :0ep :0fp '0+, v > 2eg 1- : 2ep !#v_ 4eg : fg 1- \ fp 4eg fg #v _ 3eg ! 3ep 4eg 1+ 4ep v
      v pe30 < a


      And this is an excerpt of the BrainF*ck part (a language with 8 instructions):
      >+>+<<<<<-]>>,--------- ---

      And keep in mind that the actual program is much longer than that.

      I wish I could include more, but the lameness filter won't allow me to...

      So much for nearly identical languages...
    • Perl and PHP is different. Sure they may have that dollar sign as variables, and they have C style flow control, but that's where the similarity ends.

      PHP borrows its syntax from various other languages, including Perl, but also C, Java, Javascript, etc
    • by joto (134244) on Saturday May 11 2002, @09:45PM (#3504372)
      This must be some of the worst bullshit I've heard about programming languages in quite some time.

      Did you even read the article. I'll challenge you to find languages with much more different syntax from C/Perl than Befunge-98 and Brainfuck!

      There used to be PASCAL for scientists, FORTRAN for mathematicians, BASIC for hobbyists or new programmers...

      Actually, Pascal was for education, (and systems-programming (once you added some much-needed non-standard extensions)). Fortran was for scientists (mathmaticians would probably be happier with Lisp, or something like Mathematica, only scientists needs actual numbers).

      Obj C, C, and C++ are very similar

      No, they are not. Well, ObjC and C are the most similar of the three, but modern C++ has little in common with idiomatic C. Java looks very similar to Objective C (which pretty much tells you how different C and Objective C are).

      ...and most of the new 'Basic' environments like REALBasic and VisualBasic are near clones as well.

      Maybe. My experience with VB didn't leave me thinking it was anything close to Java (or any other of the above mentioned languages). However, VB.NET is supposed to be so.

      All of today's popular coding environments could be condensed to Java, Objective C, Perl, and some form of BASIC.

      Well, if by popularity, you mean lots of users, or lots of jobs available, I am very confused why Objective C is on the list (although OS X should give it a boost). On the other hand, if you mean liked by it's users, you will hardly find any language not fitting that description. By any account, you need C++ on the list.

      But yes, I agree that such a list can be made, and mine would be: C, C++, Java, VB, Perl, COBOL, PL/SQL, HTML/XML, ASP/JSP/PHP, SAS, Python, Matlab, Fortran, Common Lisp, mostly in that order, but maybe COBOL even more to the front of the list.

      Anyway, there is no way to avoid C, C++ and or Java on the top of the list. (Which maybe was your point, but anyone taking more than a cursory glance at those languages will find that they are in fact very different from each other. They look similar on the surface, but are just as different as Pascal, Fortran and Basic).

    • by yerricde (125198) on Saturday May 11 2002, @09:02PM (#3504278) Homepage Journal

      The correct term is tetralingual, not quadrilingual.

      "Quadri" comes from Latin. "Lingual" is from Latin. "Tetra" comes from Greek. In general, a compound will be all-Greek or all-Latin, with the occasional exception such as "homosexual".

      Quadrilingual [google.com] is used in 1,210 pages, whereas tetralingual [google.com] is used in only 14.

      I assume your Game Boy reference alluded to Tetris®. In that case, the existence of Quadra [sourceforge.net] negates any "by default, go with the name of the block game" rule. In other words, you need to lay off the drugs [pineight.com] ;-)

    • http://slashdot.org/comments.pl?sid=32469&cid=3504 205

      Poster named Damiam, you are in violation of the terms of my license. You are hereby asked to cease and desist from distributing your code, and will be contacted shortly with a cease and desist letter saying the same message.

      If you do not pay royalties to me, I shall strip you of all your Karma, and fart in your general direction. :)

      By the way, if you claim prior art, this is not true. I came up with this program back in 1984 when I was banging enter on the keyboard of my Commodore Pet computer and realized that I was programming it without my knowledge.