• Complain

Mark Jason Dominus - Higher-Order Perl: Transforming Programs with Programs

Here you can read online Mark Jason Dominus - Higher-Order Perl: Transforming Programs with Programs full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2005, publisher: Morgan Kaufmann, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Mark Jason Dominus Higher-Order Perl: Transforming Programs with Programs
  • Book:
    Higher-Order Perl: Transforming Programs with Programs
  • Author:
  • Publisher:
    Morgan Kaufmann
  • Genre:
  • Year:
    2005
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Higher-Order Perl: Transforming Programs with Programs: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Higher-Order Perl: Transforming Programs with Programs" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Most Perl programmers were originally trained as C and Unix programmers, so the Perl programs that they write bear a strong resemblance to C programs. However, Perl incorporates many features that have their roots in other languages such as Lisp. These advanced features are not well understood and are rarely used by most Perl programmers, but they are very powerful. They can automate tasks in everyday programming that are difficult to solve in any other way. One of the most powerful of these techniques is writing functions that manufacture or modify other functions. For example, instead of writing ten similar functions, a programmer can write a general pattern or framework that can then create the functions as needed according to the pattern. For several years Mark Jason Dominus has worked to apply functional programming techniques to Perl. Now Mark brings these flexible programming methods that he has successfully taught in numerous tutorials and training sessions to a wider audience.
* Introduces powerful programming methodsnew to most Perl programmersthat were previously the domain of computer scientists
* Gradually builds up confidence by describing techniques of progressive sophistication
* Shows how to improve everyday programs and includes numerous engaging code examples to illustrate the methods

Mark Jason Dominus: author's other books


Who wrote Higher-Order Perl: Transforming Programs with Programs? Find out the surname, the name of the author of the book and a list of all author's works by series.

Higher-Order Perl: Transforming Programs with Programs — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Higher-Order Perl: Transforming Programs with Programs" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Recursion and Callbacks

The first advanced technique well see is recursion. Recursion is a method of solving a problem by reducing it to a simpler problem of the same type.

Unlike most of the techniques in this book, recursion is already well known and widely understood. But it will underlie several of the later techniques, and so we need to have a good understanding of its fine points.

1.1 Decimal to Binary Conversion

Until the release of Perl 5.6.0, there was no good way to generate a binary numeral in Perl. Starting in 5.6.0, you can use sprintf("%b", $num), but before that the question of how to do this was Frequently Asked.

Any whole number has the form 2k + b, where k is some smaller whole number and b is either 0 or 1. b is the final bit of the binary expansion. Its easy to see whether this final bit is 0 or 1; just look to see whether the input number is even or odd. The rest of the number is 2k, whose binary expansion is the same as that of k, but shifted left one place. For example, consider the number 37 = 2 18 + 1; here k is 18 and b is 1, so the binary expansion of 37 (100101) is the same as that of 18 (10010), but with an extra 1 on the end.

How did I compute the expansion for 37? It is an odd number, so the final bit must be 1; the rest of the expansion will be the same as the expansion of 18. How can I compute the expansion of 18? 18 is even, so its final bit is 0, and the rest of the expansion is the same as the expansion of 9. What is the binary expansion for 9? 9 is odd, so its final bit is 1, and the rest of its binary expansion is the same as the binary expansion of 4. We can continue in this way, until finally we ask about the binary expansion of 1, which of course is 1. This procedure will work for any number. To compute the binary expansion of a number n we proceed as follows:

  1. If n is 1, its binary expansion is 1, and we may ignore the rest of the procedure. Similarly, if n is 0, the expansion is simply 0. Otherwise:
  2. Compute k and b so that n = 2k + b and b = 0 or 1. To do this, simply divide n by 2; k is the quotient, and b is the remainder, 0 if n was even, and 1if n was odd.
  3. Compute the binary expansion of k, using this same method. Call the result E.
  4. The binary expansion for n is Eb.

Lets build a function called binary() that calculates the expansion. Here is the preamble, and step 1:

sub binary { my ($n) = @_; return $n if $n == 0 || $n == 1;

Here is step 2:

my $k = int( $n/2 ); my $b = $n % 2;

For the third step, we need to compute the binary expansion of k. How can we do that? Its easy, because we have a handy function for computing binary expansions, called binary() or we will once weve finished writing it. Well call binary() with k as its argument:

my $E = binary($k);

Now the final step is a string concatenation:

return $E . $b;}

This works. For example, if you invoke binary(37), you get the string 100101.

The essential technique here was to reduce the problem to a simpler case. We were supposed to find the binary expansion of a number n; we discovered that this binary expansion was the concatenation of the binary expansion of a smaller number k and a single bit b. Then to solve the simpler case of the same problem, we used the function binary() in its own definition. When we invoke binary() with some number as an argument, it needs to compute binary() for a different, smaller argument, which in turn computes binary() for an even smaller argument. Eventually, the argument becomes 1, and binary() computes the trivial binary representation of 1 directly.

This final step, called the base case of the recursion, is important. If we dont consider it, our function might never terminate. If, in the definition of binary(), we had omitted the line:

return $n if $n == 0 || $n == 1;

then binary() would have computed forever, and would never have produced an answer for any argument.

1.2 Factorial

Suppose you have a list of n different items. For concreteness, well suppose that these items are letters of the alphabet. How many different orders are there for such a list? Obviously, the answer depends on n, so it is a function of n. This function is called the factorial function. The factorial of n is the number of different orders for a list of n different items. Mathematicians usually write it as a postfix (!) mark, so that the factorial of n is n!. They also call the different orders permutations.

Lets compute some factorials. Evidently, theres only one way to order a list of one item, so 1! = 1. There are two permutations of a list of two items: A-B and B-A, so 2! = 2. A little pencil work will reveal that there are six permutations of three items:

C AB C BA A C B B C A AB C BA C

How can we be sure we didnt omit anything from the list? Its not hard to come up with a method that constructs every possible ordering, and in we will see a program to list them all. Here is one way to do it. We can make any list of three items by adding a new item to a list of two items. We have two choices for the two-item list we start with: AB and BA. In each case, we have three choices about where to put the C: at the beginning, in the middle, or at the end. There are 2 3 = 6 ways to make the choices together, and since each choice leads to a different list of three items, there must be six such lists. The preceding left column shows all the lists we got by inserting the C into AB, and the right column shows the lists we got by inserting the C into BA, so the display is complete.

Similarly, if we want to know how many permutations there are of four items, we can figure it out the same way. There are six different lists of three items, and there are four positions where we could insert the fourth item into each of the lists, for a total of 6 4 = 24 total orders:

D ABC D ACB D BAC D BCA D CAB D CBA A D BC A D CB B D AC B D CA C D AB C D BA AB D C AC D B BA D C BC D A CA D B CB D A ABC D ACB D BAC D BCA D CAB D CBA D

Now well write a function to compute, for any n, how many permutations there are of a list of n elements.

Weve just seen that if we know the number of possible permutations of n 1 things, we can compute the number of permutations of n things. To make a list of n things, we take one of the (n 1)! lists of n 1 things and insert the nth thing into one of the n available positions in the list. Therefore, the total number of permutations of n items is (n 1)!n:

sub factorial { my ($n) = @_; return factorial($n-1) * $n;}

Oops, this function is broken; it never produces a result for any input, because we left out the termination condition. To compute factorial(2), it first tries to compute factorial(1). To compute factorial(1), it first tries to compute factorial(0). To compute factorial(0), it first tries to compute factorial(-1). This process continues forever. We can fix it by telling the function explicitly what 0! is so that when it gets to 0 it doesnt need to make a recursive call:

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Higher-Order Perl: Transforming Programs with Programs»

Look at similar books to Higher-Order Perl: Transforming Programs with Programs. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Higher-Order Perl: Transforming Programs with Programs»

Discussion, reviews of the book Higher-Order Perl: Transforming Programs with Programs and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.