Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming Languages and Paradigms Functional Programming.

Similar presentations


Presentation on theme: "1 Programming Languages and Paradigms Functional Programming."— Presentation transcript:

1 1 Programming Languages and Paradigms Functional Programming

2 2 Programs are (mathematical) functions Program execution corresponds to the application of one or more functions Program units: functions and values Pure functional programming: no assignment

3 3 Function Mapping from a domain set to a range set Function definition: signature and mapping rule Signature: name, domain, range (e.g., square: integer  integer) Mapping rule: specifies range value for each domain value (e.g., square(n)  n x n)

4 4 Function Application Function application Element of the domain is specified Element replaces the parameter in the definition Other necessary function applications are carried out Yields an associated element in the range Example: square(2) yields the value 4

5 5 Variables in Functional Programs Concept of a variable in functional programs is different from a programming variable In a function definition, a variable stands for any member of the domain set During function application, a variable is bound to one specific value (and it never changes thereafter) Sometimes better to refer to variables as names

6 6 Values Traditionally, functional languages would support values for primitive types (e.g., numbers and strings) and a single high-level data structure type such as a list Values are bound to variables or names In fact, functions themselves are values

7 7 Components of a Functional Programming Language A set of data objects Numbers, lists, etc. A set of built-in functions Primarily for manipulating data objects A set of functional forms High-order functions For building new functions (e.g., defun in LISP)

8 8 Lambda Calculus A model of computation using functions Expressions of lambda calculus Identifiers (names) or constants ( e.g., y or 2 ) Function definition ( e.g., x.x*x ) Function application ( e.g. ( x.x*x 3) )

9 9 Expression Evaluation Outermost evaluation Replace arguments before evaluating them Innermost evaluation Evaluate arguments first before substituting

10 10 Decision in Functional Programming Can view “if” as a function with three arguments Condition Then-result Else-result Example: abs(x) = if(x<0, -x, x) |x| = -x if x < 0 x otherwise

11 11 “Iteration” in Functional Programming Use recursion to carry out the equivalent of an iterative process Recurrence relations are common in mathematical function specification Example: fac(n) = if(n=0,1,n*fac(n-1)) n! =1 if n = 0 n * (n-1)!otherwise

12 12 Functional Programming Environments Environments are often interpreters that are interactive Commands from user are expressions that are repeatedly evaluated Set of bindings is maintained and updated on each command

13 13 Sample Languages LISP: list processing APL: assignment-oriented but applicative ML: strongly-typed functional programming Haskell: outermost evaluation

14 14 Summary and Final Points Functional programming puts functions at the forefront of computation Elegance of mathematical functions make verifiability/provability easier (versus iteration, for example) Implementations are generally inefficient (mathematics and computer architecture not exactly compatible) Hybrid languages are becoming more popular


Download ppt "1 Programming Languages and Paradigms Functional Programming."

Similar presentations


Ads by Google