Most commonly used computer languages such as C++ and FORTRAN are imperative languages. This means that a statement is like a “sentence” in which the value of an expression or the result of a function is used in some way, such as assigning it to another variable or printing it. For example:
A = cube(3)
passes the parameter 3 to the cube function, which returns the value 27, which is then assigned to the variable A.
In a functional language, the values of functions are not assigned to variables (or stored in intermediate locations as functions are evaluated). Instead, the functions are manipu-lated directly, together with data items (atoms) arranged in lists. The earliest (and still best-known) functional lan-guage is LISP (see lisp). Programming is accomplished by defining and arranging functions until the desired process-ing is accomplished. (The decision making accomplished by branching statements in imperative languages is accom-plished by incorporating conditionals in function defini-tions.)
Many functional languages (including LISP) for conve-nience incorporate some features of imperative languages. The ML language, for example, includes data type declara-tions. A similar language, Haskell, however, eschews all such imperative features.
Applications
Functional languages have generally been used for special-ized purposes, although they can in principle perform any task that an imperative language can. APL, which is basi-cally a functional language, has devotees who appreciate its compact and powerful syntax for performing calculations (see apl). LISP and its variants have long been favored for many artificial intelligence applications, particularly natu-ral language processing, where its representation of data as lists and the facility of its list-processing functions seems a natural fit.
Proponents of functional languages argue that they free the programmer from having to be concerned with explic-itly setting up and using variables. In a functional language, problems can often be stated in a more purely mathematical way. Further, because functional programs are not orga-nized as sequentially executed tasks, it may be easier to implement parallel processing systems using functional languages.
However, critics point out that imperative languages are much closer to how computers actually work (employing actual storage locations and sequential operation) and thus produce code likely to be much faster and more efficient than that produced by functional languages.
No comments:
Post a Comment