Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Road to better programming: Chapter 4. Functional programming
By Teodor Zlatanov - 2004-02-23 Page:  1 2 3 4 5 6

Functional programming

This series of articles on developerWorks comprises a complete guide to better programming in Perl. In this fourth installment, Teodor introduces functional programming and several essential Perl idioms important for Perl programmers looking for speed and elegance in their code, such as the map() and grep() functions, and the Schwartzian and Guttman-Rosler transforms.

Functional programming (FP) can be an approach, a solution, or a religion. I prefer it to be anything but a religion, because it should be just one more tool in a programmer's arsenal.

From the "comp.lang.functional FAQ"

Functional programming is a style of programming that emphasizes the evaluation of expressions, rather than execution of commands. The expressions are formed by using functions to combine basic values. A functional language is a language that supports and encourages programming in a functional style.

It's just as good if we fake it

As I often mention, avoid using a tool just because it's there and you can use it. Hammers work on nails; screwdrivers work on screws. Know all your tools and use the right one; your life will be much easier for it.

A simplistic view of functional programming is that it deals with applying functions to values, whereas procedural (traditional) programming deals with using functions for their side effects. The references cited in the comp.lang.functional FAQ (see the Resources later in this article) are the best starting point for understanding the methodology and intent of functional programming.

In Perl, a procedural language at heart, functional programming is only possible as an approach. The actual solution will commonly use the map() or grep() functions to simulate a functional solution. The functional programming approach is valuable for three reasons. First, FP presents the programmer with a new view of the problem, and possibly a better solution will come about because of that. Second, the Schwartzian and Guttman-Rosler transforms, and many other Perl idioms, are hard to use or understand without the aid of map() and grep(). Third, implementing some algorithms without map() and grep() can slow them down significantly because function invocation in Perl is fairly expensive.

View Road to better programming: Chapter 4. Functional programming Discussion

Page:  1 2 3 4 5 6 Next Page: The map() function

First published by IBM developerWorks

Copyright 2004-2023 All rights reserved.
Article copyright and all rights retained by the author.