03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7.4. FUNCTIONAL PROGRAMMING 209<br />

7.4 Functional Programming<br />

In contrast to the procedural and object oriented paradigm, which explicitly <strong>for</strong>mulate algorithms<br />

and programs as a sequence of instructions which act on a program state, the functional<br />

paradigm uses mathematical functions <strong>for</strong> this task and <strong>for</strong>goes the use of a state altogether.<br />

There<strong>for</strong>e, there are no mutable variable and no side effects in purely functional programming.<br />

As such it is declarative in nature and relies on the language’s environment to produce an imperative<br />

representation which can be run on a physical machine. Among the greatest strengths of<br />

the functional paradigm is the availability of a strong theoretical framework of lambda calculus<br />

(cite()), which is explained in more detail in Section ref(), <strong>for</strong> the different implementations.<br />

Higher-order functions are an important concept of functional programming due to its usability<br />

in procedural languages. They were studied in lambda calculus theory well be<strong>for</strong>e the notion of<br />

functional programming existed and present the design of a number of functional programming<br />

languages, such as Scheme and Haskell.<br />

As modern procedural languages and their implementations have started to put greater emphasis<br />

on correctness, rather than raw speed, and the implementations of functional languages have<br />

begun to emphasize speed as well as correctness, the per<strong>for</strong>mance of functional languages and<br />

procedural languages has begun to converge. For programs which spend most of their time<br />

doing numerical computations, some functional languages (such as OCaml and Clean) can<br />

approach the per<strong>for</strong>mance of programs written in C speed, while <strong>for</strong> programs that handle<br />

large matrices and multidimensional databases, array functional languages (such as J and K)<br />

are usually faster than most non-optimized C programs. Functional languages have long been<br />

criticized as resource-hungry, both in terms of CPU resources and memory. This was mainly<br />

due to two things:<br />

• some early functional languages were implemented with no concern <strong>for</strong> efficiency<br />

• non-functional languages achieved speed at least in part by neglecting features such as<br />

checking of bounds or garbage collection which are viewed as essential parts of modern<br />

computing frameworks representing an overhead which was built-in to functional languages<br />

by default<br />

Since a purely functional description is free of side effects, it is a favourable choice <strong>for</strong> parallelization,<br />

as the description does not contain a state, which would require synchronization.<br />

Data related dependencies, however, must still be considered in order to ensure correct operation.<br />

Since the declarative style connected to the functional paradigm distances itself from<br />

the traditional imperative paradigm and its connection to states, input and output opearions<br />

pose a hurdle which is often addressed in a manner, which is not purely functional. As such<br />

functional interdependencies may be specified trivially, while the details how these are to be<br />

met remain opaque and as a choice to the specificc implementaiton.<br />

Last, we give an example of pure functional programming. We point out, that the next code<br />

snippet is presented in Haskel, not in C ++ syntax. The ”hello world” program in the functional<br />

programming paradigm: the factorial calculation<br />

fac :: Integer → Integer<br />

fac 0 = 1<br />

fac n | n>0 = n ∗ fac (n−1)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!