Programming Languages/Functional Languages

< Programming Languages


Functional Programming Languages

The idea of functional programming is to define rules for the working of the environment and then to let the language figure everything else out. This contrasts with procedural languages where one tells a machine exactly what to do. The classic example is a factorial function. The first step in defining a new function is to handle the trivial cases first. Factorial of 0, then, would be defined as 1. The next step is to define non-trivial cases in a way such that they will eventually resolve to the simple cases. Factorial of n, then, would be defined as n multiplied by the Factorial of one less than n. In simple cases like this, much more memory and processing time would be commited than if the function had been written using a simple 'for' loop, but functional programming has great uses. One might use a functional language as a prototype of code --it is in many cases easier write functional code and prove that it will generate the desired result than with procedural languages-- and then optimize it by using procedural techniques. Functional techniques can be very efficient at traversing binary trees.

Because lambda keeps references to the variables referenced in the outscope, yet these functions can be stored and called when the calling function has long returned, languages that use lambda have garbage collection.

Combinatory logic is an equivalent theoretical foundation, developed by Moses Schönfinkel and Haskell Curry. It was originally developed to achieve a clearer approach to the foundations of mathematics. Combinatory logic is commonly perceived as more abstract than lambda and preceded it in invention.

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.