04 Functional Programming
04 Functional Programming
Functional Programming
PS Functional Programming
Roadmap
>
>
> > > >
Functional vs. Imperative Programming Pattern Matching Referential Transparency Lazy Evaluation Recursion Higher Order and Curried Functions
O. Nierstrasz
4.2
PS Functional Programming
References
>
> >
Conception, Evolution, and Application of Functional Programming Languages, Paul Hudak, ACM Computing Surveys 21/3, 1989, pp 359-411. A Gentle Introduction to Haskell, Paul Hudak and Joseph H. Fasel
www.haskell.org/tutorial/
Report on the Programming Language Haskell 98 A Non-strict, Purely Functional Language, Simon Peyton Jones and John Hughes [editors], February 1999
www.haskell.org
>
Real World Haskell, Bryan O'Sullivan, Don Stewart, and John Goerzen
book.realworldhaskell.org/read/
O. Nierstrasz
4.3
PS Functional Programming
Roadmap
>
>
> > > >
Functional vs. Imperative Programming Pattern Matching Referential Transparency Lazy Evaluation Recursion Higher Order and Curried Functions
O. Nierstrasz
4.4
PS Functional Programming
A Bit of History
Lambda Calculus (Church, 1932-33) Lisp (McCarthy, 1960) APL (Iverson, 1962) ISWIM (Landin, 1966) formal model of computation symbolic computations with lists algebraic programming with arrays
ML (Edinburgh, 1979) SASL, KRC, Miranda lazy evaluation (Turner, 1976-85) Haskell Grand Unification of functional languages (Hudak, Wadler, et al., 1988) ...
let and where clauses equational reasoning; birth of pure functional programming ... originally meta language for theorem proving
O. Nierstrasz
4.5
PS Functional Programming
n == 0 1 n * fac (n-1)
Programs in pure functional languages have no explicit state. Programs are constructed entirely by composing expressions.
O. Nierstrasz 4.6
PS Functional Programming
Imperative Programming: > Program = Algorithms + Data Functional Programming: > Program = Functions o Functions
What is a Program? A program (computation) is a transformation from input data to output data.
O. Nierstrasz
4.7
PS Functional Programming
1. 2. 3. 4. 5.
All programs and procedures are functions There are no variables or assignments only input parameters There are no loops only recursive functions The value returned by a function depends only on the values of its parameters Functions are first-class values
O. Nierstrasz
4.8
PS Functional Programming
What is Haskell?
Haskell is a general purpose, purely functional programming language incorporating many recent innovations in programming language design. Haskell provides higher-order functions, non-strict semantics, static polymorphic typing, user-defined algebraic datatypes, pattern-matching, list comprehensions, a module system, a monadic I/O system, and a rich set of primitive datatypes, including lists, arrays, arbitrary and fixed precision integers, and floating-point numbers. Haskell is both the culmination and solidification of many years of research on lazy functional languages. The Haskell 98 report
O. Nierstrasz 4.9
PS Functional Programming
O. Nierstrasz
4.10
PS Functional Programming
Roadmap
>
>
> > > >
Functional vs. Imperative Programming Pattern Matching Referential Transparency Lazy Evaluation Recursion Higher Order and Curried Functions
O. Nierstrasz
4.11
PS Functional Programming
Pattern Matching
Haskell supports multiple styles for specifying case-based function definitions: Patterns: fac' 0 = 1 fac' n = n * fac' (n-1) -- or: fac (n+1) = (n+1) * fac n
Guards:
fac'' n
O. Nierstrasz
4.12
PS Functional Programming
Lists
Lists are pairs of elements and lists of elements: > [ ] stands for the empty list > x:xs stands for the list with x as the head and xs as the rest of the list The following short forms make lists more convenient to use > [1,2,3] is syntactic sugar for 1:2:3:[ ] > [1..n] stands for [1,2,3, ... n]
O. Nierstrasz
4.13
PS Functional Programming
Using Lists
Lists can be deconstructed using patterns: head (x:_) = x len [ ] =0 len (_:xs) = 1 + len xs prod [ ] =1 prod (x:xs) = x * prod xs
fac''' n
O. Nierstrasz
= prod [1..n]
4.14
PS Functional Programming
Roadmap
>
>
> > > >
Functional vs. Imperative Programming Pattern Matching Referential Transparency Lazy Evaluation Recursion Higher Order and Curried Functions
O. Nierstrasz
4.15
PS Functional Programming
Referential Transparency
A function has the property of referential transparency if its value depends only on the values of its parameters.
Does f(x)+f(x) equal 2*f(x)? In C? In Haskell?
Referential transparency means that equals can be replaced by equals. In a pure functional language, all functions are referentially transparent, and therefore always yield the same result no matter how often they are called.
O. Nierstrasz 4.16
PS Functional Programming
Evaluation of Expressions
Expressions can be (formally) evaluated by substituting arguments for formal parameters in function bodies:
fac 4 if 4 == 0 then 1 else 4 * fac (4-1) 4 * fac (4-1) 4 * (if (4-1) == 0 then 1 else (4-1) * fac (4-1-1)) 4 * (if 3 == 0 then 1 else (4-1) * fac (4-1-1)) 4 * ((4-1) * fac (4-1-1)) 4 * ((4-1) * (if (4-1-1) == 0 then 1 else (4-1-1) * )) 4 * ((4-1) * ((4-1-1) * ((4-1-1-1) * 1))) 24
Of course, real functional languages are not implemented by syntactic substitution ...
O. Nierstrasz 4.17
PS Functional Programming
Roadmap
>
>
> > > >
Functional vs. Imperative Programming Pattern Matching Referential Transparency Lazy Evaluation Recursion Higher Order and Curried Functions
O. Nierstrasz
4.18
PS Functional Programming
Lazy Evaluation
Lazy, or normal-order evaluation only evaluates expressions when they are actually needed. Clever implementation techniques (Wadsworth, 1971) allow replicated expressions to be shared, and thus avoid needless recalculations. So:
sqr n = n * n sqr (2+5) (2+5) * (2+5) 7 * 7 49
Lazy evaluation allows some functions to be evaluated even if they are passed incorrect or non-terminating arguments:
ifTrue True x y = x ifTrue False x y =y ifTrue True 1 (5/0) 1
O. Nierstrasz 4.19
PS Functional Programming
Lazy Lists
Lazy lists are infinite data structures whose values are generated by need:
from n = n : from (n+1)
take 0 _ take _ [ ] take (n+1) (x:xs) =[] =[] = x : take n xs take 2 (100:from 101) 100:(take 1 (from 101)) from 100 [100,101,102,103,....
NB: The lazy list (from n) has the special syntax: [n..]
O. Nierstrasz 4.20
PS Functional Programming
How would you re-write fibs so that (a+b) only appears once?
O. Nierstrasz 4.21
PS Functional Programming
primes = primesFrom 2 primesFrom n = p : primesFrom (p+1) where p = nextPrime n nextPrime n | isPrime n =n | otherwise = nextPrime (n+1) isPrime 2 = True isPrime n = notDivisible primes n notDivisible (k:ps) n | (k*k) > n = True | (mod n k) == 0 = False | otherwise = notDivisible ps n take 100 primes [ 2, 3, 5, 7, 11, 13, ... 523, 541 ]
O. Nierstrasz 4.22
PS Functional Programming
Roadmap
>
>
> > > >
Functional vs. Imperative Programming Pattern Matching Referential Transparency Lazy Evaluation Recursion Higher Order and Curried Functions
O. Nierstrasz
4.23
PS Functional Programming
Tail Recursion
Recursive functions can be less efficient than loops because of the high cost of procedure calls on most hardware. A tail recursive function calls itself only as its last operation, so the recursive call can be optimized away by a modern compiler since it needs only a single run-time stack frame:
sfac 5
fact 5
fact 5
fact 4 sfac 4
fact 5 sfac 3
fact 4
fact 3
O. Nierstrasz
4.24
PS Functional Programming
sfac 1 4
O. Nierstrasz
PS Functional Programming
Multiple Recursion
Naive recursion may result in unnecessary recalculations:
fib 1 fib 2 fib (n+2) =1 =1 = fib n + fib (n+1) NB: Not tail-recursive!
PS Functional Programming
Roadmap
>
>
> > > >
Functional vs. Imperative Programming Pattern Matching Referential Transparency Lazy Evaluation Recursion Higher Order and Curried Functions
O. Nierstrasz
4.27
PS Functional Programming
PS Functional Programming
Anonymous functions
Anonymous functions can be written as lambda abstractions. The function (\x -> x * x) behaves exactly like sqr:
sqr x = x * x sqr 10 100 (\x -> x * x) 10 100
O. Nierstrasz
4.29
PS Functional Programming
Curried functions
A Curried function [named after the logician H.B. Curry] takes its arguments one at a time, allowing it to be treated as a higher-order function. plus x y =x+y -- curried addition
plus(1,2) 3
O. Nierstrasz
4.30
PS Functional Programming
plus x y = x + y
In other words, plus is a function of one argument that returns a function as its result. plus 5 6 is the same as: (plus 5) 6
In other words, we invoke (plus 5), obtaining a function, \y -> 5 + y which we then pass the argument 6, yielding 11.
O. Nierstrasz
4.31
PS Functional Programming
O. Nierstrasz
4.32
PS Functional Programming
Currying
The following (pre-defined) function takes a binary function as an argument and turns it into a curried function: curry f a b = f (a, b) plus(x,y) inc sfac(s, n) =x+y = (curry plus) 1 = if -- not curried!
n == 0 -- not curried then s else sfac (s*n, n-1) -- bind first argument
O. Nierstrasz
4.33
PS Functional Programming
To be continued
Enumerations > User data types > Type inference > Type classes
>
O. Nierstrasz
4.34
PS Functional Programming
What is referential transparency? Why is it important? When is a function tail recursive? Why is this useful? What is a higher-order function? An anonymous
function? What are curried functions? Why are they useful? How can you avoid recalculating values in a multiply recursive function? What is lazy evaluation? What are lazy lists?
O. Nierstrasz
4.35
PS Functional Programming
constructs? When would you use patterns rather than guards to specify functions? Can you build a list that contains both numbers and functions? How would you simplify fibs so that (a+b) is only called once? What kinds of applications are well-suited to functional programming?
O. Nierstrasz
4.36
ST Introduction
License
http://creativecommons.org/licenses/by-sa/3.0/
Attribution-ShareAlike 3.0 Unported You are free: to Share to copy, distribute and transmit the work to Remix to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights.
Oscar Nierstrasz
1.37