[perl-python] 20050116 defining a function

Discussion in 'Perl Misc' started by Xah Lee, Jan 16, 2005.

  1. Xah Lee

    Xah Lee Guest

    © # the following is a example of defining
    © # a function in Python.
    ©
    © def fib(n):
    © """This prints n terms of a sequence
    © where each term is the sum of previous two,
    © starting with terms 1 and 1."""
    © result=[];a=1;b=1
    © for i in range(n):
    © result.append(b)
    © a,b=b,a+b;
    © result.insert(0,1)
    © del result[-1]
    © return result
    ©
    © print fib(6)
    ©
    © # note the use of .insert to insert 1 at
    © # the beginning of a list, and Òdel
    © # result[-1]Ó to remove the last element
    © # in a list. Also, the string
    © # immediately following the function
    © # definition is the function's
    © # documentation.
    ©
    © # the unusual syntax of a.insert() is
    © # what's known as Object Oriented syntax style.
    ©
    © # try writing a factorial function.
    ©
    © -------------------
    © # the equivalent perl version is this:
    ©
    © =pod
    ©
    © fib(n) prints n terms of a sequence where each
    © term is the sum of previous two,
    © starting with terms 1 and 1.
    ©
    © =cut
    ©
    © use strict;
    © my @result;
    © my ($a, $b);
    ©
    © sub fib($) {
    © my $n= @_[0];
    © @result=();$a=1;$b=1;
    © for my $i (1..$n){
    © push @result, $b;
    © ($a,$b)=($b,$a+$b);
    © }
    © unshift @result, 1;
    © pop @result;
    © return @result;
    © }
    ©
    © use Data::Dumper;
    © print Dumper [fib(5)];
    ©
    © # the =pod and =cut
    © # is perl's way of demarking inline
    © # documentation called POD.
    © # see Òperldoc -t perlpodÓ
    © # note: the empty line around it
    © # is necessary, at least in perl version
    © # 5.6 up to ~2002.
    ©
    © # the Òuse strict;Ó is to make perl's
    © # loose syntax stricter by enforcement.
    © # Its use is encouraged by
    © # perl gurus, but not all standard
    © # packages use it.
    ©
    © # the ÒmyÓ are used to declare variables.
    © # necessary under Òuse strict;Ó
    © # see Òperldoc -t strictÓ
    ©
    © # the $ in fib($) is there to declare
    © # that fib has a parameter of one scalar.
    © # Its use is however optional and uncommon.
    © # it is used for clarity but
    © # has also met with controversy by
    © # perl gurus as being unperl.
    © # see Òperldoc perlsubÓ for ref.
    ©
    © # the @_[0] is the first element of the
    © # array @_. The @_ array is a predefined
    © # array, holding arguments to subroutines.
    © # see Òperldoc -t perlvarÒ
    ©
    © # see
    © # perldoc -tf unshift
    © # perldoc -tf pop
    ©
    © # the last line, [fib(5)], is basically
    © # to make it a memory address of a copy of
    © # the list returned by fib(5), so that
    © # Dumper can print it.
    © # see Òperldoc -t perldataÓ or perlref
    © # for unix-styled technicalities.
    ©
    © Xah
    ©
    © http://xahlee.org/PageTwo_dir/more.html
     
    Xah Lee, Jan 16, 2005
    #1
    1. Advertising

  2. Xah Lee

    Ala Qumsieh Guest

    Xah Lee wrote:

    > © my $n= @_[0];


    Do you ever test your code before making fun of yourself in front of
    millions?

    *plonk*

    --Ala
     
    Ala Qumsieh, Jan 17, 2005
    #2
    1. Advertising

  3. Xah Lee

    Xah Lee Guest

    errata:

    * the variables in the perl section should be declared inside the
    subroutine.
    * the @_[0] should've been $_[0]

    thanks for Dave Cross for pointing them out.

    * the Mathematica Apply should be Select...
    Xah

    http://xahlee.org/PageTwo_dir/more.html
     
    Xah Lee, Jan 17, 2005
    #3
  4. Xah Lee wrote:

    > errata:
    >
    > * the variables in the perl section should be declared inside the
    > subroutine.
    > * the @_[0] should've been $_[0]
    >
    > thanks for Dave Cross for pointing them out.
    >
    > * the Mathematica Apply should be Select...
    > Xah
    >
    > http://xahlee.org/PageTwo_dir/more.html


    Here's a thought: don't post code you haven't tested!
    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Jan 17, 2005
    #4
  5. Ala Qumsieh <> wrote:
    > Xah Lee wrote:


    > *plonk*



    Man, you are way behind the curve.

    I did that over 3 years ago! [1]




    [1] Message-ID: <>

    I followed the link in that post on a hunch, and found
    he says himself that he is a troll. Go figure...

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 18, 2005
    #5
  6. Xah Lee

    Dave Cross Guest

    On Mon, 17 Jan 2005 14:26:13 -0500, Chris Mattern wrote:

    > Xah Lee wrote:
    >
    >> errata:
    >>
    >> * the variables in the perl section should be declared inside the
    >> subroutine.
    >> * the @_[0] should've been $_[0]
    >>
    >> thanks for Dave Cross for pointing them out.
    >>
    >> * the Mathematica Apply should be Select...

    >
    > Here's a thought: don't post code you haven't tested!


    Testing wouldn't have helped. The code works as originally posted - it
    doesn't doesn't work in quite the way expected.

    Dave...
     
    Dave Cross, Jan 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    450
    Peter Koch Larsen
    Jul 2, 2004
  2. Xah Lee
    Replies:
    5
    Views:
    304
    Tad McClellan
    Jan 18, 2005
  3. Replies:
    2
    Views:
    501
    John Harrison
    Nov 9, 2005
  4. Sur
    Replies:
    4
    Views:
    228
  5. Duke of Hazard

    Defining a string/array with perl characters

    Duke of Hazard, Sep 28, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    95
    Duke of Hazard
    Sep 28, 2004
Loading...

Share This Page