Loading ...
Sorry, an error occurred while loading the content.
 

Perl functional programming

Expand Messages
  • Vsevolod (Simon) Ilyushchenko
    Hi, To follow up on the discussion of Higher Order Perl , I ve tried applying its ideas. Here s a rewrite of a Python FP example.
    Message 1 of 6 , Jun 20 3:47 PM
      Hi,

      To follow up on the discussion of "Higher Order Perl", I've tried
      applying its ideas. Here's a rewrite of a Python FP example.

      http://www.simonf.com/perl/fp_perl.html

      The Python source is here:

      http://www.diveintopython.org/dynamic_functions/stage6.html

      Simon
      --

      Simon (Vsevolod ILyushchenko) simonf@...
      http://www.simonf.com

      Terrorism is a tactic and so to declare war on terrorism
      is equivalent to Roosevelt's declaring war on blitzkrieg.

      Zbigniew Brzezinski, U.S. national security advisor, 1977-81
    • Jeff Anderson
      Maybe i m just too practicle, but what advantage does FP offer over a straight foward approach, such as: use strict; use warnings; my $arg = shift; while
      Message 2 of 6 , Jun 27 8:57 AM
        Maybe i'm just too practicle, but what advantage does FP offer over a
        straight foward approach, such as:

        use strict;
        use warnings;

        my $arg = shift;

        while (<DATA>) {
        my ($pattern, $search, $replace) = split /\s+/, $_;
        if ($arg =~ /$pattern/) {
        $arg =~ s/$search/$replace/;
        die "$arg\n";
        }
        }

        __DATA__
        [sxz]$ $ es
        [^aeioudgkprt]h$ $ es
        [^aeiou]y$ y$ ies
        $ $ s

        Further ... the resulting Perl code looks far less desirable than the Python
        equivalent. Anyways ... interesting stuff none-the-less. Thanks for sharing.
        :)

        On 6/20/05, Vsevolod (Simon) Ilyushchenko <simonf@...> wrote:
        > Hi,
        >
        > To follow up on the discussion of "Higher Order Perl", I've tried
        > applying its ideas. Here's a rewrite of a Python FP example.
        >
        > http://www.simonf.com/perl/fp_perl.html
        >
        > The Python source is here:
        >
        > http://www.diveintopython.org/dynamic_functions/stage6.html
        >
        > Simon
        > --
        >
        > Simon (Vsevolod ILyushchenko) simonf@...
        > http://www.simonf.com
        >
        > Terrorism is a tactic and so to declare war on terrorism
        > is equivalent to Roosevelt's declaring war on blitzkrieg.
        >
        > Zbigniew Brzezinski, U.S. national security advisor, 1977-81
        >
        > ________________________________
        > Yahoo! Groups Links
        >
        >
        > To visit your group on the web, go to:
        > http://groups.yahoo.com/group/perlsemny/
        >
        > To unsubscribe from this group, send an email to:
        > perlsemny-unsubscribe@yahoogroups.com
        >
        > Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


        --
        jeffa


        [Non-text portions of this message have been removed]
      • Vsevolod (Simon) Ilyushchenko
        Jeff, Well, it s much more flexible - you can dynamically change what files and what rules you use. I think it s one of the main points of FP - check out the
        Message 3 of 6 , Jun 27 9:09 AM
          Jeff,

          Well, it's much more flexible - you can dynamically change what files
          and what rules you use. I think it's one of the main points of FP -
          check out the HOP book for more.

          It does look uglier than Python, but I don't think it can be done more
          elegantly (feel free to correct me). I wonder how it'll look like in Perl 6.

          Simon

          Jeff Anderson wrote on 06/27/2005 11:57 AM:
          > Maybe i'm just too practicle, but what advantage does FP offer over a
          > straight foward approach, such as:
          >
          > use strict;
          > use warnings;
          >
          > my $arg = shift;
          >
          > while (<DATA>) {
          > my ($pattern, $search, $replace) = split /\s+/, $_;
          > if ($arg =~ /$pattern/) {
          > $arg =~ s/$search/$replace/;
          > die "$arg\n";
          > }
          > }
          >
          > __DATA__
          > [sxz]$ $ es
          > [^aeioudgkprt]h$ $ es
          > [^aeiou]y$ y$ ies
          > $ $ s
          >
          > Further ... the resulting Perl code looks far less desirable than the Python
          > equivalent. Anyways ... interesting stuff none-the-less. Thanks for sharing.
          --

          Simon (Vsevolod ILyushchenko) simonf@...
          http://www.simonf.com

          Terrorism is a tactic and so to declare war on terrorism
          is equivalent to Roosevelt's declaring war on blitzkrieg.

          Zbigniew Brzezinski, U.S. national security advisor, 1977-81
        • Steven Lembark
          -- Jeff Anderson ... Sort of like relational calculus: It depends on whether you re trying to find a general solution to somthething
          Message 4 of 6 , Jun 27 10:21 AM
            -- Jeff Anderson <captvanhalen@...>

            > Maybe i'm just too practicle, but what advantage does FP offer over a
            > straight foward approach, such as:

            Sort of like relational calculus: It depends on whether
            you're trying to find a general solution to somthething
            or deal with it one-off. Unfortunately, most examples of
            anything more complicated than a one-off don't fit into
            space less than a textbook...

            Thinks like SAX handlers are a nice place for some kind
            of FP since things like nested tags may leave you with
            context-dependent handling of otherwise-identical input:

            <foo nest="1">
            <sub_foo nest="2">
            <bar nest="3">
            <foo nest="4">
            <sub_foo nest="5">>

            may leave you with different semantics for handling the
            character values or attributes at nest==1 vs. nest==3.
            Some sort of metadata driven programming is usually best
            for handling these cases (vs. hard-coding the nesting
            level attributes).


            > use strict;
            > use warnings;
            >
            > my $arg = shift;
            >
            > while (<DATA>) {
            > my ($pattern, $search, $replace) = split /\s+/, $_;
            > if ($arg =~ /$pattern/) {
            > $arg =~ s/$search/$replace/;
            > die "$arg\n";
            > }
            > }
            >
            > __DATA__
            > [sxz]$ $ es
            > [^aeioudgkprt]h$ $ es
            > [^aeiou]y$ y$ ies
            > $ $ s
            >
            > Further ... the resulting Perl code looks far less desirable than the
            > Python
            > equivalent. Anyways ... interesting stuff none-the-less. Thanks for
            > sharing.
            > :)

            You might also notice that $arg =~ /$pattern/ is
            equally true as $arg =~ s/$pattern/$replace/; you
            are also providing default values to split.

            Cleaning that up leaves you with:

            my $arg = shift;

            while( <DATA> );
            {
            my ($pattern, $search, $replace ) = split;

            $arg =~ s{$search}{$replace} and die $_
            }

            which most people can follow pretty easily: split
            the input, if the argument line munges then die
            with the modified string.

            --
            Steven Lembark 85-09 90th Street
            Workhorse Computing Woodhaven, NY 11421
            lembark@... 1 888 359 3508
          • Steven Lembark
            -- Vsevolod (Simon) Ilyushchenko ... Ugly? You can always go out of your way to write perl that s sloppy enough that it looks worse than
            Message 5 of 6 , Jun 28 5:31 AM
              -- "Vsevolod (Simon) Ilyushchenko" <simonf@...>

              > Jeff,
              >
              > Well, it's much more flexible - you can dynamically change what files
              > and what rules you use. I think it's one of the main points of FP -
              > check out the HOP book for more.
              >
              > It does look uglier than Python, but I don't think it can be done more
              > elegantly (feel free to correct me). I wonder how it'll look like in Perl
              > 6.

              Ugly? You can always go out of your way to write perl
              that's sloppy enough that it looks worse than line noise...

              Will someone please explain to me what is ugly about
              this code:

              use strict;

              my $message = shift;

              while( <DATA> )
              {
              my ($pattern, $search, $replace) = split;

              $message =~ s/$search/$replace/ and die $message;
              }

              __DATA__
              [sxz]$ $ es
              [^aeioudgkprt]h$ $ es
              [^aeiou]y$ y$ ies
              $ $ s

              If you like your code un-chunked with hanging indents,
              fine. But I'd also like to know what makes this code any
              worse than Python?

              --
              Steven Lembark 85-09 90th Street
              Workhorse Computing Woodhaven, NY 11421
              lembark@... 1 888 359 3508
            • Vsevolod (Simon) Ilyushchenko
              ... I referred to my code in relation to the original Python code. :) ... Purely visually, Perl has more special characters which make the code look ungainly
              Message 6 of 6 , Jun 28 6:21 AM
                Steven Lembark wrote on 06/28/2005 08:31 AM:
                >
                > Ugly? You can always go out of your way to write perl
                > that's sloppy enough that it looks worse than line noise...

                I referred to my code in relation to the original Python code. :)

                > Will someone please explain to me what is ugly about
                > this code:
                >
                > use strict;
                >
                > my $message = shift;
                >
                > while( <DATA> )
                > {
                > my ($pattern, $search, $replace) = split;
                >
                > $message =~ s/$search/$replace/ and die $message;
                > }
                >

                > If you like your code un-chunked with hanging indents,
                > fine. But I'd also like to know what makes this code any
                > worse than Python?

                Purely visually, Perl has more special characters which make the code
                look ungainly at first sight, IMO. Not that it counts for me or anyone
                else on this list, but the first reaction of a programmer unfamiliar
                with either language would favor the code that looks "prettier".

                Let's not go into a language war here... :)

                Simon
                --

                Simon (Vsevolod ILyushchenko) simonf@...
                http://www.simonf.com

                Terrorism is a tactic and so to declare war on terrorism
                is equivalent to Roosevelt's declaring war on blitzkrieg.

                Zbigniew Brzezinski, U.S. national security advisor, 1977-81
              Your message has been successfully submitted and would be delivered to recipients shortly.