[Oc-pm] Meeting Notes

Ben Tilly btilly at gmail.com
Fri May 25 16:35:32 PDT 2007


On 5/25/07, Pete Wilson <peter.t.wilson at gmail.com> wrote:
> Hi all,
>
> It is true that the only way to bind function arguments to lexically
> scoped variables in Perl is to use the "=" operator.  But, I just used
> assignment to bind function arguments to lexical variable.  No
> changing a variables value in a given scope/environment once it is
> set.

If you're willing to consider that, then sure, you can do it in Perl.

> My rationalization for this actually comes from previous experience
> with C++.  C++ uses the "=" operator for both assignment and
> initialization.  For example, "int I = 0;" is considered an
> initialization statement where as "I = 1;" is considered an assignment
> statement.  This doesn't map 1 to 1 with what I did in perl.  I'm not
> doing initialization I'm binding lexicaly scoped function arguments,
> but all the assignments I do in my Y combinator implementation are to
> variables immediately preceded by "my".
>
> What I currently have is a derivation of the Y combinator using the
> factorial function as an example recursive function.  The Perl file is
> currently about 200 lines; although the final combinator it's self is
> only 11 lines.   I'm currently tweaking it to make sure I understand
> what I did.  I will try to post what I have in about half an hour.

200 lines?  Seems excessive.  The following computes 5!:

  sub {
    my $builder = shift;
    sub {
      my $n = shift;
      $builder->($builder)->($n);
    }
  }->(
    sub {
      my $recurse = shift;
      sub {
        my $n = shift;
        (0 == $n) ? 1 : $n * $recurse->($recurse)->($n - 1);
      }
    }
  )->(5);

Generalizing this would take more code, but it is all a question of
how you want to generalize it.

Ben


More information about the Oc-pm mailing list