1 comp313a functional programming (1). 2 main differences with imperative languages say more about...

27
1 COMP313A Functional Programming (1)

Upload: justin-douglas-mccormick

Post on 31-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

1

COMP313AFunctional

Programming (1)

2

Main Differences with Imperative Languages

• Say more about what is computed as opposed to how

• Pure functional languages have no – variables– assignments – other side effects

• Means no loops

3

Differences…

• Imperative languages have an implicit state (state of variables) that is modified (side effect).

• Sequencing is important to gain precise, deterministic control over the state

• assignment statement needed to change the binding for a particular variable (alter the implicit state)

4

• Imperative factorial

n:=x;a:=1;while n > 0 dobegin

a:= a * n;n := n-1;

end;

5

Differences

• Declarative languages have no implicit state

• Program with expressions• The state is carried around explicitly

– e.g. the formal parameter n in factorial

• Looping is accomplished with recursion rather than sequencing

6

Haskellfac ::Int -> Intfac n | n == 0 = 1 | n > 0 = fac(n-1) *

n | otherwise = 0

Scheme (define fac (lambda (n) (if (= n 0) 1 (* n (fac (- n 1))))))

7

Advantages

• similar to traditional mathematics• referential transparency makes

programs more readable• higher order functions give great

flexibility• concise programs

8

Referential Transparency

• the value of a function depends only on the values of its parameters

for example Haskell

…x + x…where x = f a

9

Evolution of Functional languages

Lambda Calculus• Alonzo Church• Captures the essence of functional

programming• Formalism for expressing computation by

functions• Lambda abstraction

• In Scheme

• In Haskell

(x. + 1 x)

(lambda(x) (+ 1 x)))

add1 :: Int -> Intadd1 x = 1 + x

10

Lambda Calculus…

• application of expressions

• reduction rule that substitutes 2 for x in the lambda (beta reduction)

(x. + 1 x) 2

(x. + 1 x) 2 (+ 1 2) 3

(x. * x x) (+ 2 3) i.e. (+ 2 3) / x)

11

Lambda Calculus…

• (x.E) – variable x is bound by the lambda– the scope of the binding is the expression E

• (x. + y x)– (x. + y x)2 (+ y 2)

• (x. + ((y. ((x. * x y) 2)) x) y)

12

Lambda Calculus…

• Each lambda abstraction binds only one variable

• Need to bind each variable with its own lambda.

(x. (y. + x y))

13

Lisp

• McCarthy late 1950s• Used Lambda Calculus for

anonymous functions• List processing language• First attempt built on top of FORTRAN

14

LISP…• McCarthy’s main contributions were

1. the conditional expression and its use in writing recursive functions

Scheme (define fac (lambda (n) (if (= n 0) 1 (if (> n 0) (* n (fac (- n 1))) 0))))

Scheme(define fac2 (lambda (n) (cond ((= n 0) 1) ((> n 0) (* n (fac2 (- n 1)))) (else 0))))

Haskellfac ::Int -> Intfac n | n == 0 = 1 | n > 0 = fac(n-1) * n | otherwise = 0

Haskellfac :: Int -> Intfac n = if n == 0 then 1 else if n > 0 then fac(n-1) * n else 0

15

2.the use of lists and higher order operations over lists such as mapcar

Lisp…

Scheme(mymap + ’(3 4 6) ’(5 7 8))

Haskellmymap :: (Int -> Int-> Int) -> [Int] -> [Int] ->[Int]mymap f [] [] = []mymap f (x:xs) (y:ys) = f x y : mymap f xs ys

add :: Int -> Int -> Intadd x y = x + y

Scheme(define mymap (lambda (f a b) (cond ((and (null? a) (null? b)) '()) (else (cons (f (first a) (first b)) (mymap f (rest a) (rest b)))))))

Haskellmymap add [3, 4, 6] [5, 7, 8]

16

Lisp

3. cons cell and garbage collection of unused cons cells

Scheme (cons ’1 ’(3 4 5 7))

(1 3 4 5 7)

Haskellcons :: Int -> [Int] -> [Int]cons x xs = x:xs

Scheme(define mymap (lambda (f a b) (cond ((and (null? a) (null? b)) '()) (else (cons (f (first a) (first b)) (mymap f (first a) (first b)))))))

17

Lisp…

4. use of S-expressions to represent both program and data

An expression is an atom or a listBut a list can hold anything…

18

Scheme (cons ’1 ’(3 4 5 7))

(1 3 4 5 7)

Scheme(define mymap (lambda (f a b) (cond ((and (null? a) (null? b)) '()) (else (cons (f (first a) (first b)) (mymap f (first a) (first b)))))))

19

ISWIM

• Peter Landin – mid 1960s• If You See What I Mean

Landon wrote “…can be looked upon as an attempt to deliver Lisp from its eponymous commitment to lists, its reputation for hand-to-mouth storage allocation, the hardware dependent flavour of its pedagogy, its heavy bracketing, and its compromises with tradition”

20

Iswim…

• Contributions– Infix syntax– let and where clauses, including a notion of

simultaneous and mutually recursive definitions

– the off side rule based on indentation• layout used to specify beginning and end of

definitions

– Emphasis on generality • small but expressive core language

21

let in Scheme

• let* - the bindings are performed sequentially

(let* ((x 2) (y 3))

(* x y))

(let* ((x 2) (y 3))

(let* ((x 7) (z (+ x y)))

(* z x)))

22

let in Scheme

(let ((x 2) (y 3)) ?

(* x y))

• let - the bindings are performed in parallel, i.e. the initial values are computed before any of the variables are bound

(let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) ?

(* z x)))

23

letrec in Scheme

• letrec – all the bindings are in effect while their initial values are being computed, allows mutually recursive definitions

(letrec ((even? (lambda (n) (if (zero? n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (even? 88))

24

ML

• Gordon et al 1979• Served as the command language for a proof

generating system called LCF– LCF reasoned about recursive functions

• Comprised a deductive calculus and an interactive programming language – Meta Language (ML)

• Has notion of references much like locations in memory

• I/O – side effects• But encourages functional style of programming

25

ML

• Type System– it is strongly and statically typed– uses type inference to determine the type of

every expression– allows polymorphic functions

• Has user defined ADTs

26

SASL, KRC, and Miranda

• Used guards • Higher Order Functions• Lazy Evaluation• Currying

SASLfac n = 1, n = 0 = n * fac (n-1), n>0

Haskellfac n | n ==0 = 1 | n >0 = n * ( fac(n-1)

add x y = + x y

add 1

switch :: Int -> a -> a -> aswitch n x y| n > 0 = x| otherwise = y

multiplyC :: Int -> Int -> IntVersusmultiplyUC :: (Int, Int) -> Int

27

SASL, KRC and Miranda

• KRC introduced list comprehension

• Miranda borrowed strong data typing and user defined ADTs from ML

comprehension :: [Int] -> [Int]comprehension ex = [2 * n | n <- ex]