1.
ICS 313 - Fundamentals of Programming Languages 115. Functional Programming15.1 IntroductionThe design of the imperative languages is baseddirectly on the von Neumann architectureEfficiency is the primary concern, rather than thesuitability of the language for software developmentThe design of the functional languages is basedon mathematical functionsA solid theoretical basis that is also closer to the user, butrelatively unconcerned with the architecture of themachines on which programs will run
2.
ICS 313 - Fundamentals of Programming Languages 215.2 Mathematical FunctionsA mathematical function is a mapping of members of one set,called the domain set, to another set, called the range setA lambda expression specifies the parameter(s) and themapping of a function in the following formλ(x) x * x * xfor the function cube (x) = x * x * xLambda expressions describe nameless functionsLambda expressions are applied to parameter(s) by placingthe parameter(s) after the expressione.g. (λ(x) x * x * x)(3)which evaluates to 2715.2 Mathematical Functions (continued)Functional FormsA higher-order function, or functional form, is onethat either takes functions as parameters or yields afunction as its result, or bothFunction CompositionA functional form that takes two functions as parametersand yields a function whose result is a function whosevalue is the first actual parameter function applied to theresult of the application of the secondForm: h ≡ f ° gwhich means h (x) ≡ f ( g ( x))
3.
ICS 313 - Fundamentals of Programming Languages 315.2 Mathematical Functions (continued)ConstructionA functional form that takes a list of functions as parameters and yieldsa list of the results of applying each of its parameter functions to agiven parameterForm: [f, g]For f (x) ≡ x * x * x and g (x) ≡ x + 3,[f, g] (4) yields (64, 7)Apply-to-allA functional form that takes a single function as a parameter andyields a list of values obtained by applying the given function to eachelement of a list of parametersForm: αFor h (x) ≡ x * x * xα ( h, (3, 2, 4)) yields (27, 8, 64)15.3 Fundamentals of Functional Programming LanguagesThe objective of the design of a FPL is to mimic mathematicalfunctions to the greatest extent possibleThe basic process of computation is fundamentally different ina FPL than in an imperative languageIn an imperative language, operations are done and the resultsare stored in variables for later useManagement of variables is a constant concern and source of complexity forimperative programmingIn an FPL, variables are not necessary, as is the case inmathematicsIn an FPL, the evaluation of a function always produces thesame result given the same parametersThis is called referential transparency
4.
ICS 313 - Fundamentals of Programming Languages 4Functional Programming LanguagesLISPLambda notation is used to specify functions and function definitions, functionapplications, and data all have the same formSchemeA mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simplerversion than the contemporary dialects of LISPCOMMON LISPA combination of many of the features of the popular dialects of LISP around in theearly 1980sMLA static-scoped functional language with syntax that is closer to Pascal than to LISPHaskellSimilar to ML (syntax, static scoped, strongly typed, type inferencing)Different from ML (and most other functional languages) in that it is PURELYfunctional (e.g., no variables, no assignment statements, and no side effects of anykind)15.8 HaskellMost Important FeaturesUses lazy evaluation (evaluate no subexpression until the valueis needed)Has “list comprehensions,” which allow it to deal with infinitelistsExamplesFibonacci numbers (illustrates function definitions with differentparameter forms)fib 0 = 1fib 1 = 1fib (n + 2) = fib (n + 1) + fib n
5.
ICS 313 - Fundamentals of Programming Languages 515.8 Haskell (continued)Factorial (illustrates guards)fact n| n == 0 = 1| n > 0 = n * fact (n - 1)The special word otherwise can appear as a guardList operationsList notation: Put elements in bracketse.g., directions = [north, south, east, west]Length: #e.g., #directions is 4Arithmetic series with the .. operatore.g., [2, 4..10] is [2, 4, 6, 8, 10]Catenation is with ++e.g., [1, 3] ++ [5, 7] results in [1, 3, 5, 7]CAR and CDR via the colon operator (as in Prolog)e.g., 1:[3, 5, 7] results in [1, 3, 5, 7]15.8 Haskell (continued)Examples:product [] = 1product (a:x) = a * product xfact n = product [1..n]List comprehensions: set notatione.g.,[n * n | n ← [1..20]]defines a list of the squares of the first 20 positive integersfactors n = [i | i [1..n div 2],n mod i == 0]This function computes all of the factors of its given parameterQuicksort:sort [] = []sort (a:x) = sort [b | b ← x; b <= a]++ [a] ++sort [b | b ← x; b > a]
6.
ICS 313 - Fundamentals of Programming Languages 615.8 Haskell (continued)Lazy evaluationInfinite listse.g.,positives = [0..]squares = [n * n | n ← [0..]](only compute those that are necessary)e.g.,member squares 16would return TrueThe member function could be written as:member [] b = Falsemember (a:x) b = (a == b) || member x bHowever, this would only work if the parameter to squares wasa perfect square; if not, it will keep generating them forever.The following version will always work:member2 (m:x) n| m < n = member2 x n| m == n = True| otherwise = False15.8 Haskell (continued)Applications of Functional Languages:LISP is used for artificial intelligenceKnowledge representationMachine learningNatural language processingModeling of speech and visionScheme is used to teach introductory programming at a significantnumber of universitiesComparing Functional and Imperative LanguagesImperative Languages:Efficient executionComplex semanticsComplex syntaxConcurrency is programmer designedFunctional Languages:Simple semanticsSimple syntaxInefficient executionPrograms can automatically be made concurrent
Be the first to comment