Concepts of Programming Languages

(Sean Pound) #1
15.11 A Comparison of Functional and Imperative Languages 717

Following is an example of using times:

x = times.(3, 4)

This sets x to 12. The times object can be curried with the following:

times5 = times.curry.(5)

This function can be used as in the following:

x5 = times5.(3)

This sets x5 to 15.
C# includes the FindAll method of the list class. FindAll is similar
in purpose to the filter function of ML. C# also supports a generic list data
type.

15.11 A Comparison of Functional and Imperative Languages


This section discusses some of the differences between imperative and func-
tional languages.
Functional languages can have a very simple syntactic structure. The list
structure of LISP, which is used for both code and data, clearly illustrates this.
The syntax of the imperative languages is much more complex. This makes
them more difficult to learn and to use.
The semantics of functional languages is also simpler than that of the
imperative languages. For example, in the denotational semantics description
of an imperative loop construct given in Section 3.5.2, the loop is converted
from an iterative construct to a recursive construct. This conversion is unneces-
sary in a pure functional language, in which there is no iteration. Furthermore,
we assumed there were no expression side effects in all of the denotational
semantic descriptions of imperative constructs in Chapter 3. This restriction is
unrealistic, because all of the C-based languages include expression side effects.
This restriction is not needed for the denotational descriptions of pure func-
tional languages.
Some in the functional programming community have claimed that the
use of functional programming results in an order-of-magnitude increase in
productivity, largely due to functional programs being claimed to be only 10
percent as large as their imperative counterparts. While such numbers have
been actually shown for certain problem areas, for other problem areas, func-
tional programs are more like 25 percent as large as imperative solutions to the
same problems (Wadler, 1998). These factors allow proponents of functional
programming to claim productivity advantages over imperative programming
of 4 to 10 times. However, program size alone is not necessarily a good measure
of productivity. Certainly not all lines of source code have equal complexity,
Free download pdf