Posts Tagged ‘computer science’

Functional Programming Defined by Consensus

Tuesday, June 26th, 2012


Learning computer science is part of what I am up to currently. Through two classes at Udacity.com I’ve been introduced to basic programming in cs101, and then to functional programming in cs212. Both used the python which is known for its power, ease of learning, and suitability for application to a broad spectrum of problems.

The first course introduced basic concepts such as creating, assigning and storing data in memory, defining functions as blocks of code, program flow control structures, argument passing and data return, program execution and data output.

Building on that foundation, the second courses introduced additional powerful capabilities. List comprehensions, regular expressions, generator expressions, search algorithms and recursion were covered, all at breakneck speed. Fundamental to this second class though was a concept that challenged the understanding and assumptions of the students new to computer science; that was the power of the function within functional programming. The essential distinction is that, beyond merely defining a function, it thereafter can be treated as any other object that the language accommodates. This is such a powerful idea, and its implications so far reaching that there is a pedagogical imperative by which students are are first intentionally shielded from this knowledge. Computer Science professors slowly feed their students regurgitated worms until the time that their little birds are ready to be pushed from the nest; until such time in the course that students are properly prepared to absorb the full impact, utility, and implications of functional programming. Lovely image, won’t you agree? I just threw that in to see if anyone was paying attention.

For me, the concept was rather elusive at first, and still remains somewhat mysterious. So much so that it bears revisiting. So now, before my next round of online classes start, I will as an exercise attempt to define functional programming by digesting an two existing StackOverflow threads (first,second). My theory is that if I shine a light on a subject from many different directions a better understanding will emerge. This will be my first, but probably not my last attempt to reinforce my conceptual understanding of functional programming.

As gleaned from the referenced threads the following is one composite definition of functional programming, or “FP” hereafter. FP is well suited for a wide variety of problems but especially so when mathematics are involved. Because of the mathematical emphasis FP is more flexible in terms of abstraction and composition. Here abstraction means the ability to simplify problems by using FP constructs to model the problem. Composition refers to the ability to extend the power of functions by combining, nesting and extending them as permitted by the FP language of choice.

A key distinction, referring to that first mentioned above in my introduction is that FP promotes functions to first class variables. Here is where, if the professor said that on the first day of class, I would have likely said, “hey, I want to go back to my regurgitated worms.” Sorry, I could not resist.

One feature of an FP approach is to limit the use of global variables, or variables that are visible to the full scope of the program. This facilitates the ability to employ parallel execution. Typical usage of this would include graphic modeling and ray-tracing. The latter figures prominently in computer animation.

FP also implements powerful data structure capabilities. A benefit of this allows for more efficient algorithms and code than non-FP approaches. Similarly, with list comprehensions, such as provided in python, FP promotes brevity by allowing the collapse of very complex loop mechanisms into a single line list comprehension.

Generator expression in python also promote efficient memory utilization in that elements in a large set of data can be referenced by their place in the set, as if generated on demand, rather than consuming a large array as with in imperative programming.

Finally FP encourages modularity as a result of a more decouple approach than imperative programming; this enables reusability of code and improves testability as well.

This concludes my experiment in FP concept reinforcement. I have benefited from the exercise, and I hope you were not overly traumatized by the worm reference. I promise to try not to use such a revolting literary device ever again.