48
Functional Functional Programming Programming Universitatea Politehnica Bucuresti 2008-2009 Adina Magda Florea http://aimas.cs.pub.ro/ fp_09

Functional Programming

Embed Size (px)

DESCRIPTION

Functional Programming. Universitatea Politehnica Bucuresti 2008-2009 Adina Magda Florea http://aimas.cs.pub.ro/fp_09. L1 – Course content. Introduction, programming paradigms, basic concepts Introduction to Scheme Expressions, types, and functions Lists Programming techniques - PowerPoint PPT Presentation

Citation preview

Page 1: Functional Programming

Functional ProgrammingFunctional Programming

Universitatea Politehnica Bucuresti2008-2009

Adina Magda Floreahttp://aimas.cs.pub.ro/fp_09

Page 2: Functional Programming

L1 – Course content

Introduction, programming paradigms, basic concepts

Introduction to Scheme Expressions, types, and functions Lists Programming techniques Name binding, recursion, iterations, and

continuations Introduction to Haskell Problem solving paradigms in FP

Page 3: Functional Programming

Course materials

Scheme resources Almost all you want to know and find about Schemehttp://www.schemers.org/ Development and execution environmenthttp://www.drscheme.org/ Books and research articles on Schemehttp://library.readscheme.org/page2.html R. Kent Dybvig

The Scheme Programming Language, Second Edition – online bookhttp://www.scheme.com/tspl2d/index.html

Haskellhttp://www.haskell.org/ Haskell language designhttp://haskell.readscheme.org/lang_sem.html

Page 4: Functional Programming

Requirements Laboratory: min 6 Laboratory assignments Homeworks Final examGrading Laboratory assignments and homewwork

50% Final exam 50%

Page 5: Functional Programming

Lecture No. 1 Introduction to FP Mathematical functions LISP Introduction to Scheme

Page 6: Functional Programming

1. Introduction to FP The design of the imperative languages is based

directly on the von Neumann architecture Efficiency is the primary concern, rather than the

suitability of the language for software development The design of the functional languages is based

on mathematical functions A solid theoretical basis that is also closer to the

user, but relatively unconcerned with the architecture of the machines on which programs will run

Page 7: Functional Programming

1.1 Principles of FP treats computation as evaluation of mathematical

functions (and avoids state) data and programs are represented in the same

way functions as first-class values

– higher-order functions: functions that operate on, or create, other functions

– functions as components of data structures lamda calculus provides a theoretical framework

for describing functions and their evaluation it is a mathematical abstraction rather than a

programming language

Page 8: Functional Programming

1.2 History lambda calculus (Church, 1932) simply typed lambda calculus (Church, 1940) lambda calculus as prog. lang. (McCarthy(?), 1960,

Landin 1965) polymorphic types (Girard, Reynolds, early 70s) algebraic types ( Burstall & Landin, 1969) type inference (Hindley, 1969, Milner, mid 70s) lazy evaluation (Wadsworth, early 70s) Equational definitions Miranda 80s Type classes Haskell 1990s

Page 9: Functional Programming

1.3 Varieties of FP languages

typed (ML, Haskell) vs untyped (Scheme, Erlang)

Pure vs Impure impure have state and imperative features pure have no side effects, “referential

transparency” Strict vs Lazy evaluation

Page 10: Functional Programming

1.4 Declarative style of programming

Declarative Style of programming - emphasis is placed on describing what a program should do rather than prescribing how it should do it.

Functional programming - good illustration of the declarative style of programming.

A program is viewed as a function from input to output.

Logic programming – another paradigm A program is viewed as a collection of logical rules

and facts (a knowledge-based system). Using logical reasoning, the computer system can derive new facts from existing ones.

Page 11: Functional Programming

1.5 Functional style of programming

A computing system is viewed as a function which takes input and delivers output.

The function transforms the input into output . Functions are the basic building blocks from

which programs are constructed. The definition of each function specifies what

the function does. It describes the relationship between the input

and the output of the function.

Page 12: Functional Programming

Examples

Describing a game as a function Text processing Text processing: translation Compiler

Page 13: Functional Programming

1.6 Why functional programming

Functional programming languages are carefully designed to support problem solving.

There are many features in these languages which help the user to design clear, concise, abstract, modular, correct and reusable solutions to problems.

The functional Style of Programming allows the formulation of solutions to problems to be as easy, clear, and intuitive as possible.

Since any functional program is typically built by combining well understood simpler functions, the functional style naturally enforces modularity.

Page 14: Functional Programming

Why functional programming Programs are easy to write because the system relieves the

user from dealing with many tedious implementation considerations such as memory management, variable declaration, etc .

Programs are concise (typically about 1/10 of the size of a program in non-FPL)

Programs are easy to understand because functional programs have nice mathematical properties (unlike imperative programs) .

Functional programs are referentially transparent , that is, if a variable is set to be a certain value in a program; this value cannot be changed again. That is, there is no assignment but only a true mathematical equality.

Page 15: Functional Programming

Why functional programming

Programs are easy to reason about because functional programs are just mathematical functions;

hence, we can prove or disprove claims about our programs using familiar mathematical methods and ordinary proof techniques (such as those encountered in high school Algebra).

For example we can always replace the left hand side of a function definition by the corresponding right hand side.

Page 16: Functional Programming

1.7 Examples of FP languages

Lisp (1960, the first functional language….dinosaur, has no type system)

Hope (1970s an equational fp language) ML (1970s introduced Polymorphic typing systems) Scheme (1975, static scoping) Miranda (1980s equational definitions, polymorphic typing Haskell (introduced in 1990, all the benefits of above +

facilities for programming in the large.) Erlang (1995 - a general-purpose concurrent programming

language and runtime system, introduced by Ericsson) The sequential subset of Erlang is a functional language, with

dynamic typing.

Page 17: Functional Programming

2. Mathematical functions

Def: A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set

A lambda expression specifies the parameter(s) and the mapping of a function in the following form

(x) x * x * x for the function cube (x) = x * x * x

Page 18: Functional Programming

Mathematical functions

Lambda expressions describe nameless functions

Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression

e.g. ((x) x * x * x)(3) which evaluates to 27

Page 19: Functional Programming

Mathematical functions

Functional Forms Def: A higher-order function, or functional form, is

one that either takes functions as parameters or yields a function as its result, or both

Page 20: Functional Programming

Functional forms

1. Function Composition A functional form that takes two functions as

parameters and yields a function whose value is the first actual parameter function applied to the application of the second

Form: h f ° g which means h (x) f ( g ( x)) For f (x) x * x * x and g (x) x + 3, h f ° g yields (x + 3)* (x + 3)* (x + 3)

Page 21: Functional Programming

Functional forms

2. Construction A functional form that takes a list of functions as

parameters and yields a list of the results of applying each of its parameter functions to a given parameter

Form: [f, g] For f (x) x * x * x and g (x) x + 3, [f, g] (4) yields (64, 7)

Page 22: Functional Programming

Functional forms

3. Apply-to-all A functional form that takes a single function as a

parameter and yields a list of values obtained by applying the given function to each element of a list of parameters

Form: For h (x) x * x * x ( h, (3, 2, 4)) yields (27, 8, 64)

Page 23: Functional Programming

3. LISP Lambda notation is used to specify functions and

function definitions. Function applications and data have the same form.

e.g., If the list (A B C) is interpreted as data it is a simple list of three atoms, A, B, and C If it is interpreted as a function application, it means that the function named A is applied to the two parameters, B and C The first LISP interpreter appeared only as a

demonstration of the universality of the computational capabilities of the notation

Page 24: Functional Programming

4. Introduction to Scheme

A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than the contemporary dialects of LISP

Invented by Guy Lewis Steele Jr. and Gerald Jay Sussman

Emerged from MIT Originally called Schemer

Shortened to Scheme because of a 6 character limitation on file names

Page 25: Functional Programming

Introduction to Scheme

Designed to have very few regular constructs which compose well to support a variety of programming styles Functional, object-oriented, and imperative

All data type are equal What one can do to one data type, one can do to all

data types

Page 26: Functional Programming

Introduction to Scheme

Uses only static scoping Functions are first-class entities

They can be the values of expressions and elements of lists

They can be assigned to variables and passed as parameters

Page 27: Functional Programming

Introduction to Scheme

Primitive Functions

1. Arithmetic: +, -, *, /, ABS, SQRT, REMAINDER, MIN, MAX

e.g., (+ 5 2) yields 7

Page 28: Functional Programming

Introduction to Scheme

2. QUOTE -takes one parameter; returns the parameter without evaluation QUOTE is required because the Scheme interpreter,

named EVAL, always evaluates parameters to function applications before applying the function. QUOTE is used to avoid parameter evaluation when it is not appropriate

QUOTE can be abbreviated with the apostrophe prefix operator

e.g., '(A B) is equivalent to (QUOTE (A B))

Page 29: Functional Programming

Introduction to Scheme

3. CAR takes a list parameter; returns the first element of that list

e.g., (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A B)4. CDR takes a list parameter; returns the list

after removing its first element e.g., (CDR '(A B C)) yields (B C) (CDR '((A B) C D)) yields (C D)

Page 30: Functional Programming

Introduction to Scheme

5. CONS takes two parameters, the first of which can be either an atom or a list and the second of which is a list; returns a new list that includes the first parameter as its first element and the second parameter as the remainder of its result

e.g., (CONS 'A '(B C)) returns (A B C)

Page 31: Functional Programming

Introduction to Scheme

6. LIST - takes any number of parameters; returns a list with the parameters as elements

Page 32: Functional Programming

Introduction to Scheme

Lambda Expressions Form is based on notation

e.g., (LAMBDA (L) (CAR (CAR L))) L is called a bound variable Lambda expressions can be applied e.g., ((LAMBDA (L) (CAR (CAR L))) '((A B) C D))

Page 33: Functional Programming

Introduction to Scheme

A Function for Constructing Functions DEFINE - Two forms: 1. To bind a symbol to an expression e.g., (DEFINE pi 3.141593) (DEFINE two_pi (* 2 pi))

Page 34: Functional Programming

Introduction to Scheme

2. To bind names to lambda expressions e.g., (DEFINE (cube x) (* x x x))

Example use: (cube 4)

Page 35: Functional Programming

Introduction to Scheme

Evaluation process (for normal functions): 1. Parameters are evaluated, in no particular order2. The values of the parameters are substituted into

the function body3. The function body is evaluated4. The value of the last expression in the body is the

value of the function(Special forms use a different evaluation process)

Page 36: Functional Programming

Introduction to Scheme

Example:(DEFINE (square x) (* x x)) (DEFINE (hypotenuse side1 side1) (SQRT (+ (square side1) (square side2))))

Page 37: Functional Programming

Introduction to Scheme Example:(define(list-sum lst) (cond ((null? lst) 0) ((pair? (car lst)) (+(list-sum (car lst)) (list-sum (cdr lst))))

(else (+ (car lst) (list-sum (cdr

lst))))))

Page 38: Functional Programming

Some conventions

Naming Conventions A predicate is a procedure that always returns a

boolean value (#t or #f). By convention, predicates usually have names that end in `?'.

A mutation procedure is a procedure that alters a data structure. By convention, mutation procedures usually have names that end in `!'.

Page 39: Functional Programming

Cases

Uppercase and Lowercase Scheme doesn't distinguish uppercase and

lowercase forms of a letter except within character and string constants; in other words, Scheme is case-insensitive.

For example, Foo is the same identifier as FOO, but 'a' and 'A' are different characters.

Page 40: Functional Programming

Predicate functions

Predicate Functions: (#t is true and #f or ()is false)1. EQ? takes two symbolic parameters; it returns #T

if both parameters are atoms and the two are the same

e.g., (EQ? 'A 'A) yields #t (EQ? 'A '(A B)) yields ()

Note that if EQ? is called with list parameters, the result is not reliable

Also, EQ? does not work for numeric atoms

Page 41: Functional Programming

Predicate functions

Predicate Functions: 2. LIST? takes one parameter; it returns #T if the

parameter is a list; otherwise()3. NULL? takes one parameter; it returns #T if the

parameter is the empty list; otherwise() Note that NULL? returns #T if the parameter is()

4. Numeric Predicate Functions =, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?, NEGATIVE?

Page 42: Functional Programming

Control flow

Control Flow 1. Selection- the special form, IF (IF predicate then_exp else_exp)

e.g., (IF (<> count 0) (/ sum count) 0 )

Page 43: Functional Programming

Control flow Control Flow

2. Multiple Selection - the special form, COND General form: (COND (predicate_1 expr {expr}) (predicate_1 expr {expr}) ... (predicate_1 expr {expr}) (ELSE expr {expr}) ) Returns the value of the last expr in the first pair whose predicate evaluates to true

Page 44: Functional Programming

Example

(DEFINE (compare x y) (COND ((> x y) (DISPLAY “x is greater than y”))

((< x y) (DISPLAY “y is greater than x”))

(ELSE (DISPLAY “x and y are equal”))

) )

Page 45: Functional Programming

Example

1. member - takes an atom and a simple list; returns #T if the atom is in the list; () otherwise

(DEFINE (member atm lis) (COND ((NULL? lis) '()) ((EQ? atm (CAR lis)) #T) ((ELSE (member atm (CDR lis))) ))

Page 46: Functional Programming

Example

2. equalsimp - takes two simple lists as parameters; returns #T if the two simple lists are equal; () otherwise

(DEFINE (equalsimp lis1 lis2) (COND ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((EQ? (CAR lis1) (CAR lis2)) (equalsimp(CDR lis1)(CDR lis2))) (ELSE '())))

Page 47: Functional Programming

Example

3. equal - takes two general lists as parameters; returns #T if the two lists are equal; ()otherwise

(DEFINE (equal lis1 lis2) (COND ((NOT (LIST? lis1))(EQ? lis1 lis2)) ((NOT (LIST? lis2)) '()) ((NULL? lis1) (NULL? lis2)) ((NULL? lis2) '()) ((equal (CAR lis1) (CAR lis2)) (equal (CDR lis1) (CDR lis2))) (ELSE '())))

Page 48: Functional Programming

Example

4. append - takes two lists as parameters; returns the first parameter list with the elements of the second parameter list appended at the end

(DEFINE (append lis1 lis2) (COND ((NULL? lis1) lis2) (ELSE (CONS (CAR lis1) (append (CDR lis1) lis2)))))