Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Perl's functional features compared with Ruby

by gunzip (Pilgrim)
on Jan 17, 2014 at 09:29 UTC ( [id://1070921] : perlquestion . print w/replies, xml ) Need Help??

gunzip has asked for the wisdom of the Perl Monks concerning the following question:

I've had to spend quite a bit of time with Ruby lately and coming from Perl it's easy to see how much Matz has borrowed. I've also been drawn to functional programming and am about to embark on my first Common Lisp journey after re-reading Higher Order Perl. This book left me wondering which functional programming features Perl supports that Ruby doesn't as MJD didn't feel qualified to comment, I read somewhere. I've heard it argued both ways, for example, that Ruby's blocks are not real closures like Perl's lexical closures. So, can any of you who know Perl and Ruby well enlighten me as to what advantages Perl has in functional programming compared with Ruby?

Replies are listed 'Best First'.
Re: Perl's functional features compared with Ruby
by davido (Cardinal) on Jan 17, 2014 at 16:44 UTC

    I believe the passage you're referring to from Higher Order Perl is this one:

    If you pick up a good book about Lisp, there will be a section that describes Lisp's good features. For example, the book Paradigms of Artificial Intelligence Programming, by Peter Norvig, includes a section titled What Makes Lisp Different? that describes seven features of Lisp. Perl shares six of these features; C shares none of them. There are big, important features, features like first-class functions, dynamic access to the symbol table, and automatic storage management. Lisp programmers have been using these features since 1957. They know a lot about how to use these language features in powerful ways. If Perl programmers can find out the things that Lisp programmers already know, they will learn a lot of things that will make their Perl programming jobs easier.

    In that passage MJD mentions seven features of Lisp. He enumerates three of them as "big" features:

    • First-class functions.
    • Dynamic access to the symbol table.
    • Automatic storage management.

    I don't know Ruby, so I would have to ask someone who does (and it sounds like you've spent more time with it than I have): Does Ruby have First Class Functions? The table on the Wikipedia page to which I linked seems to indicate that Ruby's semantics don't provide as much flexibility in this regard as Perl.

    The next one is "dynamic access to the symbol table". I'm hesitant to say that Ruby doesn't provide full access to the symbol table, but this StackOverflow answer seems to indicate that there is a certain degree of introspection available, and I believe there's an equivalent to symbolic references in Ruby. I don't know if it rises to the level of flexibility (and danger) that Perl provides, but it might, and someone who knows Ruby better could say for sure.

    The last one is automatic storage management. Perl is a reference counting language. So is Ruby. Both will have the potential for trouble with circular references.

    At one point I was interested in what the other categories were, so I looked for the book that MJD refers to. Here's the list, copied word for word from the book:

    • Built-in Support for Lists
    • Automatic Storage Management
    • Dynamic Typing
    • First-class Functions
    • Uniform Syntax
    • Interactive Environment
    • Extensibility
    • History

    (The list is actually eight, including History.)

    Uniform Syntax was interesting to me. I found this article from drdobbs that demonstrated what that refers to, and guess what? Perl has that (it's how we do objects).

    Dynamic typing is a no-brainer; of course Perl has dynamic typing. Built-in support for lists: I think the swap idiom demonstrates that: ( $b, $a ) = ( $a, $b );, though Lisp seems to take this support to an impressive level. Interactive environment? Well, not like Lisp, but we do have Repl's available. ...but not like Lisp, and I doubt there are many languages that do it to the degree that Lisp does. Extensability: We have the ability to pull in modules and classes. Modules can have their own namespaces, or they can infect other namespaces if we want them to. We have XS. Perl is pretty extensible, but Lisp is designed such that extensibility is the only way to actually accomplish anything. A Lisp program is the art of manipulating the syntax tree. Perl possesses some of the extensibility features, but again Lisp is on a whole different level.

    As for history, it's hard to compete with a language that's been around since the 50's.

    So now that you have the complete list, you can do your research and report back to us on how Ruby fares against Lisp, and against Perl's Lispy strengths.


      > Built-in support for lists: I think the swap idiom demonstrates that: ( $b, $a ) = ( $a, $b );, though Lisp seems to take this support to an impressive level.

      I think here you are falling into a definition trap!

      You are showing a Perl list assignment...

      But IMHO in LISP-context list mostly means linked list, Perl doesn't support this datatype cause most features can be efficiently mimicked with it's arrays (push, pop, ...).

      You may remember that MJD simulated linked lists in his book with chained arrays of two element [$value,$a_ref] where $a_ref always pointing to the next chain link.

      One advantage of linked lists over arrays come when splitting them for concurrent processing.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        It's possible that I don't know what I don't know. ;) ...or that I don't know enough about what "built-in support for lists" means to two different authors coming from different language backgrounds, so you're probably right that I'm falling into a definition trap.

        There are enough fundamental differences between the two languages that paradigms differ, so it's hard to pin down exactly how their features overlap. I suspect that MJD's opinion would differ from Norvig, and I can't hope to bring them together.


Re: Perl's functional features compared with Ruby
by LanX (Saint) on Jan 17, 2014 at 16:29 UTC
    I don't feel qualified for an in depth discussion of Ruby, some snippets I heard for further discussions.

    1. Ruby (like Python) doesn't declare variables like Perl does with my or JS with var

      Variables are implicitly declared by first initialization. This leads to ambiguities if you try to assign to a closed over variable. (and sabotages any attempt to realize strict)

    2. Ruby has (had?) problems with blocks in blocks.

      IIRC it wasn't clear if a closed over variable belonged to the top or the middle scope. This lead to changes between different versions and therefore to incompatibilities.

    3. Ruby has some syntactic sugar in FP where Perl needs more syntax.

      refer to RFC: Simulating Ruby's "yield" and "blocks" in Perl as an example.

      or compare lamdas in Perl { my ($x,$y) = @_; ... } and Ruby  { | x,y |  ... }

    4. Ruby and Perl are on a semantic level barely different.

      Quoting Guido: "From a 10 km above perspective Python, Perl and Ruby are the same language".

      I'd like to add from a 10 m perspective this already applies to Perl and Ruby!

      Matz based Ruby mostly on Perl attempting a more pleasant syntax and later spending much time denying it.

      This includes FP!

    Maybe stackoverflow is a better place to ask and attract more polyglot crowd for in detail analysis.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Perl's functional features compared with Ruby
by PerlSufi (Friar) on Jan 17, 2014 at 15:16 UTC
    I taught myself a small amount of Ruby. If perl and python got drunk and made a baby, that is what Ruby feels like to me. I'm not super skilled in either enough to tell you too much, except that I think that a huge advantage of perl is CPAN. Hands down, CPAN is one of the greatest things about perl. I'm sure the number of modules that CPAN has compared to Ruby is significantly more. I found Ruby's way of installing modules to be annoying. Also, I'm not sure if Ruby supports things like $_ and @_ ?
    UPDATE: I found this node which may be relevant: Perl Vs Ruby
    One more relevant link from the Ruby website -that might help- is here:
Re: Perl's functional features compared with Ruby
by Anonymous Monk on Jan 17, 2014 at 17:49 UTC