Programming Languages and Paradigms Functional Programming 1 Functional

  • Slides: 14
Download presentation
Programming Languages and Paradigms Functional Programming 1

Programming Languages and Paradigms Functional Programming 1

Functional Programming n n Programs are (mathematical) functions Program execution corresponds to the application

Functional Programming n n 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 2

Function n n Mapping from a domain set to a range set Function definition:

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

Function Application n Function application n n Element of the domain is specified Element

Function Application n Function application n n 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 4

Variables in Functional Programs n n Concept of a variable in functional programs is

Variables in Functional Programs n n 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 5

Values n Traditionally, functional languages would support values for n n primitive types (e.

Values n Traditionally, functional languages would support values for n n 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 6

Components of a Functional Programming Language n A set of data objects n n

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

Lambda Calculus n n A model of computation using functions Expressions of lambda calculus

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

Expression Evaluation n Outermost evaluation n n Replace arguments before evaluating them Innermost evaluation

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

Decision in Functional Programming n Can view “if” as a function with three arguments

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

“Iteration” in Functional Programming n n n Use recursion to carry out the equivalent

“Iteration” in Functional Programming n n n 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 11

Functional Programming Environments n n n Environments are often interpreters that are interactive Commands

Functional Programming Environments n n n 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 12

Sample Languages n n LISP: list processing APL: assignment-oriented but applicative ML: strongly-typed functional

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

Summary and Final Points n n Functional programming puts functions at the forefront of

Summary and Final Points n n 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 14