CT006!3!3 ADPLC 02 FunctionalProgramming Part 1
CT006!3!3 ADPLC 02 FunctionalProgramming Part 1
Functional Programming
Part I: Introduction
Learning Outcomes
Functional programming basics Compare functional programming to other programming paradigms with regard to fundamental characteristics Give the advantages and disadvantages of functional programming
Title of Slides
Key Terms
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
Module Code and Module Title
Function Value Expression Operator Definition Function Definition Functional Program Pattern Matching Recursion Tuple Side effect
Title of Slides Slide 4 (of 42)
Functions
A function is a way of computing a result from a list of arguments. f(x) = sin x / cos x button(mouse click) = close window
x is the argument (angle) A function that closes a window based on a button click
Operations such as multiplication, subtraction, etc. can be unary or binary, using operators to achieve a result
-5 (negating a value is a unary operation) 5 + 2 (adding two values is a binary operation)
Module Code and Module Title Title of Slides Slide 7 (of 42)
Haskell
Names start with a small letter, and are made up of letters and digits.
area :: Int
area = 41 * 37
An expression says how the value is computed.
* Basic Types: Int ,Float, Char, Bool Structured Types: lists, tuples, functions, classes (we will discuss classes and generics more in Week 7)
Module Code and Module Title Title of Slides Slide 8 (of 42)
Function Definitions
A function definition specifies how the result is computed from the arguments.
arg
arg
result
Function types specify the types of the arguments and the result. The body specifies how the result is computed.
Function Notation
Function arguments need not be enclosed in brackets! Example: average :: Float -> Float -> Float
average x y = (x + y) / 2
Calls: average 2 3 2.5
6.5
Functional Programming
A functional program consists mostly of function definitions. Simple functions are used to define more complex ones, which are used to define still more complex ones, and so on. Finally, we define a function to compute the output of the entire program from its inputs. If you can write function definitions, you can write functional programs!
Module Code and Module Title Title of Slides Slide 11 (of 42)
Types
From mathematics, were used to functions whose arguments and results are numbers. In programs, we usually work with much richer types. Some types are built in to programming languages (basic types usually), whereas others are defined by programmers (through structs, classes, etc.)
Module Code and Module Title Title of Slides Slide 12 (of 42)
Integer Type
Whole numbers (between -2^31 and 2^31-1) [32-bit]
1, 2, 3, 4
:: Int
Some operations:
2+3 2*3 5 6 div 7 2 mod 7 2 3 1
2^3
8
integer division!
Title of Slides
Some operations:
2.5 + 1.5 3 - 1.2 1.4142^2 4.0 1.8 1.99996
1/3
0.333333
sin (pi/4)
0.707107
Title of Slides
List Type
A list of values enclosed in square brackets.
[1,2,3,4,5]
head [1,2,3]
last [1,2,3]
Module Code and Module Title Title of Slides
1
3
Slide 15 (of 42)
Title of Slides
String Type
Hello! :: String
Hello ++ World
Hello World
show (2+2)
Is 2+2 equal to 4?
Module Code and Module Title Title of Slides Slide 17 (of 42)
Command Type
A command to write Hello! to myfile. The type of a command which produces no value.
Quick Quiz
If myfile contains Hello!,
is readFile myfile equal to Hello!?
NO!
The result of a function depends only on its arguments; Hello! cannot be computed from myfile.
Title of Slides
What if we already know the value of fac (n-1)? = 1 * 2 * * (n-1) * n = fac (n-1) * n
Then fac n
Title of Slides
n 0 1 2 3 4 ...
fac n 1 1 2 6 24
Title of Slides
Title of Slides
fac 0 = 1
fac n | n > 0 = fac (n-1) * n
fac 4
?? 4 == 0 ?? 4 > 0
False True
fac 2 * 3 * 4 fac 1 * 2 * 3 * 4
fac (4-1) * 4
fac 3 * 4
fac 0 * 1 * 2 * 3 * 4
1*1*2*3*4 24
Title of Slides
Quick Quiz
Define a function power so that power x n == x * x * * x n times
(Of course, power x n == x^n, but you should define power without using ^).
power x 0 = 1 Dont forget the base case!
Title of Slides
Tuples
A tuple is a value made up of several other values, namely its components.
Examples: A point in the plane (x,y) A point in space (x,y,z)
A purchase
The null tuple
Module Code and Module Title
Tuple Types
A tuple type specifies the type of each component.
Examples:
Title of Slides
Type Definitions
We can give names to types with a type definition:
type Purchase = (String, Int) All types start with a capital letter. Now Purchase and (String, Int) are interchangeable: dagens :: Purchase dagens = (Dagens Lunch, 55)
Title of Slides
name (s, i) = s
A pattern which must match the actual argument.
Title of Slides
Title of Slides
Using Lists
We often need to count:
[1..10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1,3..10] = [1, 3, 5, 7, 9] Example: fac :: Int -> Int
Using Lists
We often do the same thing to every element of a list:
Example:
[2, 4, 6]
Title of Slides Slide 32 (of 42)
Example:
factors :: Int -> [Int]
factors 12
Module Code and Module Title
[2, 3, 4, 6, 12]
Title of Slides Slide 34 (of 42)
Haskells powerful list constructions often offer a simpler alternative to using recursion.
But not always! Recursion is a powerful and general tool -therefore harder to use than special purpose tools, when they are applicable.
Title of Slides
Title of Slides
Efficiency
Functional programming languages have automatic memory management with garbage collection, in contrast to older imperative languages like C and Pascal.
Coding style
Imperative programs emphasize a series of steps taken by a program, while functional programs emphasize the composition and arrangement of functions
Module Code and Module Title Title of Slides Slide 37 (of 42)
Advantages of using FP
Unit Testing
Since every symbol in FP is final (non-mutable), no function can ever cause side effects (no unexpected changes in state). You can test every function in your program only worrying about its arguments! In an imperative language, checking a return value is not sufficient it may modify external state.
Debugging
A bug in a functional program does not depend on unrelated code paths that were executed before it. A wrong return value is always wrong (idempotence), whereas in an imperative language, a wrong value may crop up intermittently.
Module Code and Module Title Title of Slides Slide 38 (of 42)
Advantages of using FP
Concurrency
In FP, you dont have to worry about deadlocks or race conditions. This makes FP ideal for massively concurrent applications, like telecommunication switches (Ericsson developed Erlang for just this purpose!)
Disadvantages of FP
Most of the criticism leveled at functional programming has to do with difficulty in learning and lazy evaluation
Learning curve: functional programming is more mathematically-oriented, and may thus present a challenge to imperative programmers used to that paradigm Lazy evaluation, a feature supported in most FP languages, can complicate I/O and debugging. Execution speed of Haskell and some other FP languages has also stymied adoption.
Module Code and Module Title Title of Slides Slide 40 (of 42)
Q&A
Any Questions?
Title of Slides
Next Session
Fundamental functional programming characteristics
Higher-order functions Pure functions and referential transparency Functional composition
Title of Slides