1 programming languages and paradigms functional programming

14
1 Programming Languages and Paradigms Functional Programming

Upload: darren-ball

Post on 31-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Programming Languages and Paradigms Functional Programming

1

Programming Languagesand Paradigms

Functional Programming

Page 2: 1 Programming Languages and Paradigms Functional Programming

2

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

Page 3: 1 Programming Languages and Paradigms Functional Programming

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)

Page 4: 1 Programming Languages and Paradigms Functional Programming

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

Page 5: 1 Programming Languages and Paradigms Functional Programming

5

Variables inFunctional 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

Page 6: 1 Programming Languages and Paradigms Functional Programming

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

Page 7: 1 Programming Languages and Paradigms Functional Programming

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)

Page 8: 1 Programming Languages and Paradigms Functional Programming

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) )

Page 9: 1 Programming Languages and Paradigms Functional Programming

9

Expression Evaluation Outermost evaluation

Replace arguments before evaluating them

Innermost evaluation Evaluate arguments first before

substituting

Page 10: 1 Programming Languages and Paradigms Functional Programming

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

Page 11: 1 Programming Languages and Paradigms Functional Programming

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 = 0n * (n-1)! otherwise

Page 12: 1 Programming Languages and Paradigms Functional Programming

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

Page 13: 1 Programming Languages and Paradigms Functional Programming

13

Sample Languages LISP: list processing APL: assignment-oriented but

applicative ML: strongly-typed functional

programming Haskell: outermost evaluation

Page 14: 1 Programming Languages and Paradigms Functional Programming

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