gold96

Upload: mohit-gaharana

Post on 05-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 gold96

    1/3

    Functional Programming Languages

    BENJAMIN GOLDBERG

    New York Univers ity goldberg @cs.nyu.edu

    Functional programming languages area class of languages designed to reflectthe way people think mathematically,rather than reflecting the underlyingmachine. Functional languages arebased on the lambda calculus, a simplemodel of computation, and have a solidtheoretical foundation that allows oneto reason formally about the programswritten in them. The most commonly

    used functional languages are StandardML, Haskell, and pure Scheme (a dia-lect of LISP), which, although they dif-fer in many ways, share most of theproperties described here. For a com-plete description of these languages andof functional programming in general,see Bird and Wadler [1988], Paulson[1991], Sussman and Abelson [1985],Hudak et al. [1992], Milner et al. [1990],

    Artificial Intelligence Laboratory[1992], and Hudak [1989].

    In contrast to the usual imperativelanguages (e.g., C, Fortran, and Ada), inwhich variables represent cells in mem-

    ory that can be modified using the as-signment operator (or :), functionallanguages view the use of the opera-tor as an expression of an equation. Forexample, if a functional program con-tains the declaration

    le t x f y

    then this would introduce the name xand assert that the equation x f(y)is true. There is no notion of a memorycell, and certainly no notion that some-how later in the program x mightchange (so that the equation would be-come false).

    If one were to write

    le t x x 1

    in a functional program, this would rep-resent an equation with no finite solu-tion (and would either be rejected by acompiler or result in a nonterminatingcomputation), whereas in C this wouldincrement the contents of the memorycell denoted by x.

    Function names are introduced in asimilar way. The declaration

    le t f x, y x y

    introduces the function f and statesthat f(x, y) and x y are equal, forany x and y. The expression on theright-hand side, the body of f, cannot bea sequence of statements modifying thevalues of x and y (and perhaps othervariables). As in mathematics, a functionis a single-valued relation such that,given the same argument(s), it will returnthe same result. This is certainly not thecase in the imperative programs.

    Because variables in a functional pro-

    gram cannot be modified, repetitionmust be expressed in a functional pro-gram via recursion rather than throughthe use of loops. Consider the factorialfunction, defined formally as:

    n!1 if n 0

    n n 1 ! otherwise

    In a functional language the executable-definition of factorial is generally writ-ten as

    let factorial n

    if n 0 then 1

    else n*factorial n 1

    Copyright 1996, CRC Press.

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

  • 7/31/2019 gold96

    2/3

  • 7/31/2019 gold96

    3/3

    NON-STRICT FUNCTIONAL LANGUAGES

    In most programming languages, afunction call of the form

    f(e1, . . . , e n)

    causes the argument expressions,e1. . .en, to be evaluated before the bodyof the function f is executed. This isalso the case in ML and Scheme. How-ever, in a class of functional languagescalled non-strict functional languages,of which Haskell is the most popular, nofunction evaluates its arguments unlessthey are needed. For example, the func-tion f defined by

    le t f x,y,z if x 0

    then y 1 else z

    always evaluates its first, parameter, x,but only one of y or z will be evaluated.Thus, in the call

    f 4, g 3 , h 2

    the expression g(3) will not be evalu-ated.

    Non-strictness is attractive for tworeasons. First, it frees the programmerfrom worrying about various issues ofcontrol, such as choosing the correctorder of evaluation among various ex-

    pressions. In a non-strict language, anexpression program wont be evaluatedunless it is needed. For example, in aproducer-consumer problem, the pro-ducer is guaranteed to produce onlywhat the consumer needs.

    Another feature of non-strictness isthat it allows the construction of infi-nite data structures. To see this, con-sider the recursive definition

    let ones 1 : : on es

    where the :: operator constructs a listwhose first element is the left operand, in

    this case 1, and whose subsequent ele-ments come from the right operand, inthis ones. That is, ones is a list that isrecursively defined to be 1 followed by allthe element of ones. Clearly, the onlysolution to this equation is if ones is theinfinite list of 1s. In a strict language,where :: requires the value of its argu-

    ments, the evaluation of the right-handside of the equation would never termi-nate. However, in a non-strict language,the :: does not evaluate its operands until

    they are actually needed. Ultimately, onlythose elements of ones that are requiredby other parts of the programs will becomputed. The rest of ones (which is infi-nite) would be left uncomputed.

    RESEARCH ISSUES IN FUNCTIONAL

    LANGUAGES

    The functional language research com-munity is very active in a number ofareas. Of particular, interest is improv-ing the speed of functional languageimplementations. There are two pri-mary approaches: through compiler-

    based program analysis and optimiza-tion techniques, and through theexecution of functional programs onparallel computers. Another area of re-search attempts to increase the expres-siveness of functional languages for ap-plications in which the notion of stateand the change of state (through assign-ment) is seen as necessary in conven-tional programs. New constructs havebeen proposed that, although they ap-pear to be side-effect operators such asarray updates, actually preserve the ref-erential transparency property.

    REFERENCES

    ARTIFICIAL INTELLIGENCE LABORATO RY 1992.Report on the algorithmic language scheme.Tech. Rep., Cambridge, MA, (Nov.), revised.

    BIRD, R. AND WADLER , P. 1988. Introduction toFunctional Program ming. Prentice Hall,Englewood Cliffs, NJ.

    HUDAK, P., ET AL. 1992. Report on the program-ming language Haskell. SIGPLAN Not. 27, 5,Section R.

    HUDAK, P. 1989. The conception, evolution, andapplication of functional programming lan-guages. ACM Comput. Surv. 21, 3, 359411.

    MILNER, R . , TOFTE, M., AND HARPER, R . 1 99 0.

    The Definition of Standard ML. MIT Press,Cambridge, MA.

    PAULSON , L. C. 1991. ML for the Working Pro-gramme r. Cambridge University Press, Cam-bridge, UK.

    SUSSMAN, G. AND ABELSON, H. 1985. Structureand Interpretation of Computer Programs.MIT Press, Cambridge, MA.

    Functional Programming Languages 251

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