0% found this document useful (0 votes)
40 views

Function Programing Language

Function programing language

Uploaded by

Acc Curious
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Function Programing Language

Function programing language

Uploaded by

Acc Curious
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Functional Programming Languages

BENJAMIN GOLDBERG
New York University ^[email protected]&

Functional programming languages are in a functional program, this would rep-


a class of languages designed to reflect resent an equation with no finite solu-
the way people think mathematically, tion (and would either be rejected by a
rather than reflecting the underlying compiler or result in a nonterminating
machine. Functional languages are computation), whereas in C this would
based on the lambda calculus, a simple increment the contents of the memory
model of computation, and have a solid cell denoted by x.
theoretical foundation that allows one Function names are introduced in a
to reason formally about the programs
similar way. The declaration
written in them. The most commonly
used functional languages are Standard let f~x, y! 5 x 1 y
ML, Haskell, and “pure” Scheme (a dia-
lect of LISP), which, although they dif- introduces the function f and states
fer in many ways, share most of the that f(x, y) and x 1 y are equal, for
properties described here. For a com- any x and y. The expression on the
plete description of these languages and right-hand side, the body of f, cannot be
of functional programming in general, a sequence of statements modifying the
see Bird and Wadler [1988], Paulson
values of x and y (and perhaps other
[1991], Sussman and Abelson [1985],
Hudak et al. [1992], Milner et al. [1990], variables). As in mathematics, a function
Artificial Intelligence Laboratory is a single-valued relation such that,
[1992], and Hudak [1989]. given the same argument(s), it will return
In contrast to the usual imperative the same result. This is certainly not the
languages (e.g., C, Fortran, and Ada), in case in the imperative programs.
which variables represent cells in mem- Because variables in a functional pro-
ory that can be modified using the as- gram cannot be modified, repetition
signment operator 5 (or :5), functional must be expressed in a functional pro-
languages view the use of the 5 opera- gram via recursion rather than through
tor as an expression of an equation. For the use of loops. Consider the factorial
example, if a functional program con- function, defined formally as:
tains the declaration

let x 5 f~y!
then this would introduce the name x
n!5 H 1 if n 5 0
n~n – 1!! otherwise
and assert that the equation x 5 f(y)
is true. There is no notion of a memory In a functional language the executable-
cell, and certainly no notion that some- definition of factorial is generally writ-
how later in the program x might ten as
change (so that the equation would be-
come false). let factorial~n!5
If one were to write
if n 55 0 then 1
let x 5 x 1 1 else n*factorial~n 2 1!

Copyright © 1996, CRC Press.

ACM Computing Surveys, Vol. 28, No. 1, March 1996


250 • Benjamin Goldberg

and follows directly from the formal def- In this case, the parameters to com-
inition (where 55 is the equality com- pose, f, and g, must both be functions.
parison operator). In addition, the value returned by
This is in contrast to the C version compose, namely h, is also a function,
defined by the equation h(x) 5
int fac~int n! f(g(x)).
$int prod 5 1; As another example of the use of
for ~int i 5 0; higher-order functions, consider again
the factorial function. It might be ar-
i ,5 n; i11) gued that the formal definition of facto-
prod 5 prod * i ; rial given above was tailored to suit the
} recursive nature of the definition of fac-
torial in the functional language, and
which can be understood only by tracing that a more reasonable and common
the sequence of modifications to the vari- definition of factorial is
ables prod and i during the iteration.
Functional languages exhibit a prop-

Pi
n
erty called referential transparency,
which essentially means that “like can n!5
be replaced by like.” For example, the i51
expression

f~y! 1 f~y! The product operator, &, is a very useful


operator that has the general form:
is equivalent to

P f~i!
n
let x 5 f~y!
in x 1 x
i5m
in which the two original occurrences of
f(y) are replaced by x. This follows
directly from the fact that the declara- for some initial value m, some final
tion x5 f(y) denotes an equation in a value n, and some function f. In a func-
functional language, but certainly tional language, & can easily be written
would not be the case in an imperative as a higher-order function of three pa-
language. In an imperative language, rameters, m, n, and f, defined by
the first call to f might change the
value of a variable used in the second prod~m, n, f !
call to f. This kind of behavior, known 5 if m 55 n then f~m!
as a side-effect, cannot occur in a func-
else f~m! p prod~m, n, f!
tional language.
where 55 is the equality comparison
operator. Thus, factorial can be defined
HIGHER-ORDER FUNCTIONS
as:
Most functional languages support func-
tions that operate over functions—that let fac~n! 5 let f~i! 5 i
is, functions that take other functions in prod~1,n,f!
as parameters and/or return functions
as values. A simple example is the com- and the power function, computing xn,
pose function, defined as can be defined as

let compose~f,g!5let h~x!5f~g~x!! let power~x,n! 5 let f~i! 5 x


in h in prod~1,n,f!

ACM Computing Surveys, Vol. 28, No. 1, March 1996


Functional Programming Languages • 251

NON-STRICT FUNCTIONAL LANGUAGES ments, the evaluation of the right-hand


side of the equation would never termi-
In most programming languages, a nate. However, in a non-strict language,
function call of the form the :: does not evaluate its operands until
f(e1, . . . , e n) they are actually needed. Ultimately, only
those elements of ones that are required
causes the argument expressions, by other parts of the programs will be
e1. . .en, to be evaluated before the body computed. The rest of ones (which is infi-
of the function f is executed. This is nite) would be left uncomputed.
also the case in ML and Scheme. How-
ever, in a class of functional languages RESEARCH ISSUES IN FUNCTIONAL
called non-strict functional languages, LANGUAGES
of which Haskell is the most popular, no
function evaluates its arguments unless The functional language research com-
they are needed. For example, the func- munity is very active in a number of
tion f defined by areas. Of particular, interest is improv-
ing the speed of functional language
let f~x,y,z! 5 if x 55 0 implementations. There are two pri-
mary approaches: through compiler-
then y 1 1 else z based program analysis and optimiza-
always evaluates its first, parameter, x, tion techniques, and through the
but only one of y or z will be evaluated. execution of functional programs on
Thus, in the call parallel computers. Another area of re-
search attempts to increase the expres-
f~4, g~3!, h~2!% siveness of functional languages for ap-
plications in which the notion of state
the expression g(3) will not be evalu- and the change of state (through assign-
ated. ment) is seen as necessary in conven-
Non-strictness is attractive for two tional programs. New constructs have
reasons. First, it frees the programmer been proposed that, although they ap-
from worrying about various issues of pear to be side-effect operators such as
control, such as choosing the correct array updates, actually preserve the ref-
order of evaluation among various ex- erential transparency property.
pressions. In a non-strict language, an
expression program won’t be evaluated
unless it is needed. For example, in a REFERENCES
producer-consumer problem, the pro- ARTIFICIAL INTELLIGENCE LABORATORY 1992.
ducer is guaranteed to produce only Report on the algorithmic language scheme.
what the consumer needs. Tech. Rep., Cambridge, MA, (Nov.), revised.
Another feature of non-strictness is BIRD, R. AND WADLER, P. 1988. Introduction to
that it allows the construction of infi- Functional Programming. Prentice Hall,
nite data structures. To see this, con- Englewood Cliffs, NJ.
sider the recursive definition HUDAK, P., ET AL. 1992. Report on the program-
ming language Haskell. SIGPLAN Not. 27, 5,
let ones 5 1 :: ones Section R.
HUDAK, P. 1989. The conception, evolution, and
where the :: operator constructs a list application of functional programming lan-
whose first element is the left operand, in guages. ACM Comput. Surv. 21, 3, 359 – 411.
this case 1, and whose subsequent ele- MILNER, R., TOFTE, M., AND HARPER, R. 1990.
The Definition of Standard ML. MIT Press,
ments come from the right operand, in Cambridge, MA.
this ones. That is, ones is a list that is
PAULSON, L. C. 1991. ML for the Working Pro-
recursively defined to be 1 followed by all grammer. Cambridge University Press, Cam-
the element of ones. Clearly, the only bridge, UK.
solution to this equation is if ones is the SUSSMAN, G. AND ABELSON, H. 1985. Structure
infinite list of 1’s. In a strict language, and Interpretation of Computer Programs.
where :: requires the value of its argu- MIT Press, Cambridge, MA.

ACM Computing Surveys, Vol. 28, No. 1, March 1996

You might also like