Springer International Publishing AG 2016
Vicen Torra Scala: From a Functional Programming Perspective Lecture Notes in Computer Science 9980 10.1007/978-3-319-46481-7_1
1. An Introduction to Functional Programming Languages
Functional programming is a programming paradigm that has one of its roots in the programming language LISP . LISP, which stands for LISt Processing , was created in 1958 by John McCarthy . Its main characteristic is that computation is in terms of functions and recursion. Syntaxis in LISP is based on the use of a prefix notation and the parenthesis, no much syntactic sugar is used. For example, the function to compute the factorial can be written as follows in LISP:
The theoretical basis of functional programming is
-calculus , developed by A. Church in the 30s. The development of
-calculus was parallel (or a little earlier [16, 29]) to the development of Turing machines by A. Turing . Both were developed as computational models and were proven equivalent from the point of view of the functions they can compute. They were independently used to prove the Entscheidungsprobleme (decision problem). While Turing machines rely on the concept of state and transition functions between states,
-calculus relies on the concept of rewriting.
Functional programming sees programs as functions, and functions are decomposed into other functions. In pure functional programming the only value that the function computes is what it returns, there are not side effects, and the input values are not modified.
For example, a typical implementation of the factorial in an imperative language is as follows.
Observe that variables i and result change their values while the loop is executed. Compare that with the variable n in the recursive definition of factorial. Note that the value n does not change.
So, a main difference between functional programming and imperative programming is that in the latter, programming is achieved by means of a modification of the variables in the program. This corresponds to changing the states, as in the Turing machine.
1.1 Main Characteristics of Functional Programming Languages
We have underlined above that functional programming has as its main characteristic in that programs are based on the definition of functions. Functions are the main elements in programs. The main properties of functional programming languages include the following (we include the section were these concepts are studied).
Expressions without side effects (Sect. )
First-class functions (Sect. ). This includes
Higher-order functions (Sect. )
Recursion (Sect. )
Immutable data structures (Sect. )
Lazy evaluation (Chap. )
Do not require tail-recursive optimization (Sect. )
This compares with the main characteristics of imperative programming languages.
Table compares functional programming and imperative programming . The table also includes the logic programming paradigm .
Table 1.1
Differences between the functional, logic and imperative paradigms.
| Functional | Logic | Imperative |
---|
A program as a | Function | Relationship | Command |
Building blocks | Expressions (evaluation) | Horn clauses (true/false?) | Assignment (execution) |
Program Construction | Composing functions | Defining facts and rules | Sequences of commands |
Variables | Immutable | Immutable | Mutable |
(let x be) | (let x be) | (memory cell) |
Repetition | Recursion | Recursion | Loop |
1.2 Some Functional Programming Languages
In this section we review briefly four functional languages that have had a strong influence in the development of this type of languages. The list of functional programming languages is, however, very large and includes e.g. Miranda, Hope, and Erlang.
1.2.1 LISP
This is the classical functional programming language. It was created by J. McCarthy 1958 and described in [12]. See [13] for details on its creation. It received influence from the Information Processing Language (a language created between 1955 and 1956), which already implemented concepts as recursion and list-processing. This language is still alive and used today and has influenced indirectly most functional programming languages and directly the language Scheme .
1.2.2 FP
This language was proposed by J. Backus and it is a kind of Extreme Functional Programming language, with no variables. The internal product IP of two vectors is defined as follows.
Def IP
/+ o
x o trans
Here, Trans is the transpose of the two vectors of the input (seen as a matrix). Then, we apply the product to all pairs of numbers and finally we add them.
Another example is the product of two matrices. Their definition is as follows.
Def MM
(
IP) o (
distl) o distr o [1, trans o 2]
The language FP is described by Backus (well known for the development of the language FORTRAN and the BNF - Backus-Naur form ) in [2]. Dijkstra presented in 1979 (see [5]) a critic of the paper by Backus [2].
1.2.3 Standard ML (SML)
This is a strongly (statically) typed functional programming language. This language is able to deduce the type of objects and functions. SML permits to define algebraic data types easily. This is discussed in Sect. (examples in SML will be given).