Hacker News new | past | comments | ask | show | jobs | submit login
Hyperpolyglot: PHP, Perl, Python, Ruby (hyperpolyglot.org)
311 points by tathagatadg on Aug 13, 2011 | hide | past | favorite | 54 comments



That these languages are so similar a table like this is possible leads one to think those programmers who consider themselves "polyglot" by programming in a subset of these aren't really "polyglot" at all.


Go to http://hyperpolyglot.org/ directly.

Included there:

    Scripting Languages:         PHP, Perl, Python, Ruby
    Embeddable Languages:        Tcl, Lua, JavaScript, Io
    Shell Languages:             Bash, Zsh, AppleScript, PowerShell
    C Style Languages:           C, C++, Objective C, Java, C#
    Pascal Style Languages:      Pascal, Ada, PL/SQL, SQL/PSM
    Lisp Dialects:               Common Lisp, Scheme, Clojure, Emacs Lisp
    Type Inference Languages:    Standard ML, OCaml, Scala, Haskell
    Declarative Languages:       Prolog, Erlang, Oz
    Concatenative Languages:     Forth, PostScript, Factor
    Computer Algebra Software:   Mathematica, Sage, Maxima
    Numerical Analysis Software: MATLAB, R


I would have liked for Go to have been included, (C style languages) not to mention Smalltalk.


I'd like to add Go, but I have a couple of reservations. First, I can't edit the page. Second, the list of features in each table is so specific. Go has a lot that the C-like languages in that table don't and vice versa. It seems there aren't enough axes along which to adequately compare.


Smalltalk definitely belongs on the list, it's the only one that takes "object-oriented" to its logical conclusion (dispatch can replace control structures). I'd suggest omitting Elisp and ObjC—they're both lackluster imitations of better languages that will do far more for the way you think, unless you happen to be developing for their captive platform (Emacs and iOS, respectively).


> Smalltalk definitely belongs on the list, it's the only one that takes "object-oriented" to its logical conclusion (dispatch can replace control structures).

i believe io does this as well.


Unless I misunderstand what you mean, don't a host of languages do this? io, ruby, javascript?


Ruby & Javascript have syntax defined in their parsers for control structures. for eg:

  # Ruby
  if someThing() 
    doThis()
  else
    doThisInstead()
  end

  # Javascript/Perl/etc
  if (someThing()) { doThis() }
  else { doThisInstead() }
Whereas Io & Smalltalk requires no new syntax and uses objects & messages:

  # Io
  if(someThing, doThis, doThisInstead)

  # Io like Ruby,etc
  if(something) then(doThis) else(doThisInstead)

  # Io like Smalltalk
  someThing ifTrue(doThis) ifFalse(doThisInstead) 
  
  # Smaltalk
  someThing ifTrue: [doThis] ifFalse: [doThisInstead].


As an aside, Haskell wouldn't need syntax for something like `if', but has it as a convenience. There's no syntax for looping, you can do that with higher-order functions.


I'm pretty sure it use to have Smalltalk on there when I last looked. Its been updated a bit so must of got removed :(


Ah didn't notice that. This site suddenly got a whole lot more awesome :)


These tables are comprehensive, but the authors don't seem to actually understand the languages' idioms.

Over on the ISWIM-family page the Haskell "command line args" program is ten lines long, when it could be:

  import System
  main = getArgs >>= mapM_ putStrLn
I wish these were written by people who knew the languages.


You can't create 'idiomatic polyglots'. You just hack things together until they sorta work, for more than few languages this basically involves large strings and lots of backslashes, or base64 or something like that. (Writing a polyglot that does something more clever is obviously much better style, but it's incredibly hard.)


"Polyglot" in this case is referring to a person who can write in multiple languages, not a program written in multiple languages.


The site doesn't mention which meaning of "polyglot" the people behind it had in mind.

I assumed it was for people writing "polyglot" toy programs, because it'd be very useful resource.

I very rarely encounter uses of the term "polyglot" referring to people knowing large number of programming languages, so I didn't think of this use. Looking at it now, from this perspective, I have to say that I don't think it would be useful for these people, not enough depth.


> I assumed it was for people writing "polyglot" toy programs, because it'd be very useful resource.

It isn't so in this case, as the code for each language is clearly different. It's more of a 'construct translator' kind of thing.


> the code for each language is clearly different.

It's always the case. That's why writing them is interesting.


Different definitions. You seem to be thinking of programs that are valid in multiple languages. The page is more of a dictionary of language constructs.


> The page is more of a dictionary of language constructs.

Which would come incredibly useful if you wanted to write a polyglot program.

Why else would you use a reference like this? If you wanted to learn a language, or had to hack something in a language you don't know, you'll be better off using resources specific to given language(s).


One would use this when writing a new program or modifying an existing one in a language one doesn't know, but when one does know another one well. Seems quite obvious.


Maybe you could contact them with the suggestion?

(There's at least couple more errors on the Haskell column, a typo, and a missing idiom for their 'each' example, which would be 'mapM_ print', basically).


Neat table, one thing about PHP's member variables compared to Python/Ruby seems inaccurate though. In the example "define class", the member variable $value is declared private and then accessed through a getter and a setter, while the corresponding Python and Ruby examples use a public variable and hence look cleaner. The equivalent PHP version should be more along the lines of:

  class Int {
    function __construct($int = 0) {
      $this->value = $int;
    }
  }
instead of the ridiculously verbose thing they posted there.


Speaking of ridiculously verbose, if they're going to pull in Moose to do OO in Perl they could at least use it's ability to provide default arguments instead of abusing BUILDARGS:

   package Int;
   use Moose;
   has 'value' => ( is => 'rw', isa => 'Int', default => 0 );
   no Moose;
Of course, Moose (while awesome) is hardly the only approach to OO in Perl:

   package Int;
   
   sub new {
       my $cls = shift;
       $cls = ref($cls) || $cls;
       my $val = shift or 0;
       bless \$val, $cls;
   }

   sub getValue { ${$_[0]} }
   1;
Rather than wrapping the scalar int inside another data structure to be treated as an "object" it's possible to just take a reference and use that as our object. So in this case getValue is really just a verbose way to dereference ($int->getValue vs. $$int).

If you don't care about inheritance you could also take out the first two lines of ``new`` and just have:

   package Int;
   sub new { bless \($_[0] or 0) }
   sub getValue { ${$_[0]} }
   1;
Disclaimer: this kind of code is usually considered terrible Perl style and why people have such a hang up about the language, don't take this as idiomatic

Edit: Just noticed that they seem to think instance variables in Perl are private by default. Not sure where they got that idea? I guess Moose attributes are read-only by default, but that's hardly the same thing.


The Ruby example actually involves generating the getter and setter at runtime via attr_accessor.

In contrast to Python, in Ruby all instance member variables are private.


Yes, but functionally the Ruby example is the equivalent of a public member variable. Since those are public by default in PHP, there is no need to complicate the code by first making it private and then implementing a getter and a setter.


This is a kind of a semantic game. You might say they're "equivalent," but you're still doing different things — and different is very rarely truly equal. For example, the Ruby accessors could be hooked or altered later without changing the interface. Not so if you use a public variable (which Ruby doesn't have, for this very reason). The only language I know of where public variables and accessors are truly interchangeable is Scala.


Everything is a semantics game: meaning matters. This is not about playing "gotcha" with trivialities. I'd like to convince you that there is a significant difference or bias about the way those languages are being showcased.

Isn't the whole point of this table to compare equivalent snippets of code? You're right, those languages do things very differently from one another - especially when it comes to OO. In this case, the stated goal was to define a class that exposes a public property. In this I believe PHP and probably Perl were represented badly.

If you don't believe me, look at the Python example: it's the exact same implementation I translated into PHP syntax above. So my terse implementation is an exact match for the Python example and it's a functional match for the Ruby example. Hence, I can't help but think that PHP and Perl were misrepresented by making them jump through unnecessary hoops that do nothing but make the language look more complicated.


I agree with your general point, but feel I should point out that this is possible in python using properties:

   class Int(object):
       def __init__(self, value):
           self.value = value

       @property
       def value(self):
           print("Accessing value")
           return self.value

       @value.setter
       def value(self, value):
           print("Setting value")
           self.value = value


Some of the PHP stuff, as others have noted with Python, etc., is misleading or outdated.

Backticks work:

    echo `ls`;
Constants can be declared globally:

    const PI = 3.14;
Anonymous functions can be declared without create_function:

    $f = function($x) { return $x*$x; }
I'm sure there are more. That said, I love these sorts of side-by-side comparisons.


There is a lot of non-idiomatic Ruby as well


Also it doesn't tell you that 'and' and 'or' are not equal to && and || in Ruby. http://hyperpolyglot.org/scripting#logical-operators


http://rosettacode.org/wiki/Rosetta_Code may be more comprehensive although not as immediately accessible


Neat, ridiculously comprehensive table. A few "warnings" I found in the first 10%:

> Python: Statement separator: ; or sometimes newline

More like newline or sometimes ;. When was the last time you saw a Python script with a semicolon?

> Python: Multiline comment: '''comment line\nanother line'''

That's a multiline string.


The Python bits do not seem to be written by a Python programmer, at least things like the above and this "package management" entry make little sense:

  $ python
  >>> help('modules')
  download pkg with matching python version
  $ tar xf libxml2-python-2.6.0.tar.gz
  $ cd libxml2-python-2.6.0
  $ sudo python setup.py install	
That's just bizarre.


Yeah I came here to point this out. I'd understand if pip weren't mentioned, but easy_install has been the standard for many years.


I might be incorrect regarding Perl's "scan" function, but I think you can implement it like this:

  my $s = "foo bar baz";
  my @b = $s =~ /\w+/g;
Granted, this is not a scanf, but neither are PHP/Python/Ruby examples.


x.succ is not the Ruby equivalent of ++$x in PHP (or Perl). x.succ returns x + 1 (by default, on integers) but does not increment x as ++$x does (since numbers are immutable and that's what "x" is giving us in Ruby). The closest match would be x += 1 (shorthand for x = x + 1). I don't see x = x.succ in common usage but that would work too.


Yeah, #succ is something else. For example, if your object supports #succ then you can use it in ranges.

e.g., (1..10), but also ("apple".."kiwis") or ('A'..'Z').

In this case, String#succ returns a string with the last alphanumeric character transformed into its successor, e.g., "apple".succ == "applef".

The only time I've used it is implementing the range interface; I've never called it directly, let alone to increment an integer.


I assume you mean "apple".succ == "applf" ?


Yes


I don't disagree with what you're saying, but it does mention in the comment that x is not mutated.


I totally didn't notice that note but the page doesn't seem to have been updated since then :-)

I still don't see why it doesn't just use the common syntax though that does the same thing. It's intriguing that the Python one has no example for that row. Perhaps they are specifically looking for idiomatic techniques, even if they're not commonly used.

I note that the page is on a wiki so I could fix it, but given it's been there for years, I suspect there's a good reason no-one has already made the change.


As a Ruby noob and without researching too much, here's my question: would x.succ! do it?


Not if x were a regular old Fixnum.

The reason is that x would be referring to a number like 1, 42, or whatever, and numbers are immutable in Ruby. You can't "change" a number in-place. If you could change Ruby's concept of 1 into 2, then 1 would cease to exist and that would be a Bad Thing™ :-)


I think this shows that much of the time, the choice of open source scripting language isn't going to make or break a project. Ditto for well-maintained web frameworks.

http://www.slideshare.net/joestump/starting-your-startup/65


Well it may not make or break a start-up but it definitely help push it one direction or the other

As much as I love Perl, if I want local partners I'd better choose Ruby


Nice! I was going to suggest adding Lua and Tcl but they're available on the Embedded page. The link in the title goes to the Scripting page.


REPL for Python: ipython


needs numpy filled out (for MATLAB/R) but otherwise really cool!


python, statement separator: not "sometimes", but "usually"

python, multiline comment: not quite. print '''foo''' outputs "foo". This is a string literal, and """foo""" is more popular.


Under variable interpolation, there is nothing listed for python, but the equivalent is this:

    count = 3
    item = "ball"
    print('%(count)s %(item)ss' % locals())


Python dosen't have NULL/NIL?


Python has "None".


It has no null??! Who's on first?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: