By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

[perl-python] 20050116 defining a function

P: n/a
© # 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
© xa*@xahlee.org
© http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Xah Lee wrote:
© my $n= @_[0];


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

*plonk*

--Ala

Jul 18 '05 #2

P: n/a
Ala Qumsieh wrote:
© my $n= @_[0];


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


this perl usability study is getting more and more interesting. who
needs television?

</F>

Jul 18 '05 #3

P: n/a
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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #4

P: n/a
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
xa*@xahlee.org
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?"
Jul 18 '05 #5

P: n/a
Ala Qumsieh <no******@email.com> wrote:
Xah Lee wrote: *plonk*

Man, you are way behind the curve.

I did that over 3 years ago! [1]


[1] Message-ID: <7f**************************@posting.google.com >

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
ta***@augustmail.com Perl programming
Fort Worth, Texas
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.