Functional Programming Lecture Revision Notes
Functional Programming Lecture Revision Notes
Notes
1. Introduction to Functional Programming
Key Concepts:
Advantages:
2. Pure Functions
Definition:
A function that always produces the same output for the same input and has no side effects.
Characteristics:
Deterministic
No state changes
No I/O operations
Example:
haskell
Copy
-- Pure function
add :: Int -> Int -> Int
add x y = x + y
3. Immutability
Definition:
Once a value is created, it cannot be changed.
Benefits:
Example:
haskell
Copy
-- Immutable list operation
addToList :: [Int] -> Int -> [Int]
addToList xs x = xs ++ [x] -- Creates a new list
4. Higher-Order Functions
Definition:
map
filter
fold (reduce)
Examples:
haskell
Copy
-- map example
doubleList :: [Int] -> [Int]
doubleList = map (*2)
-- filter example
evenNumbers :: [Int] -> [Int]
evenNumbers = filter even
-- fold example
sumList :: [Int] -> Int
sumList = foldl (+) 0
5. Recursion
Definition:
A method of solving problems by breaking them down into smaller, self-similar subproblems.
Types:
Direct recursion
Indirect recursion
Tail recursion
Example:
haskell
Copy
-- Factorial function using recursion
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
-- Tail-recursive version
factorialTail :: Integer -> Integer -> Integer
factorialTail acc 0 = acc
factorialTail acc n = factorialTail (acc * n) (n - 1)
6. Pattern Matching
Definition:
A mechanism for checking a value against a pattern and, optionally, binding variables to
successful matches.
Uses:
Function definitions
Case expressions
List comprehensions
Example:
haskell
Copy
-- Pattern matching in function definition
isZero :: Int -> Bool
isZero 0 = True
isZero _ = False
7. Lazy Evaluation
Definition:
A strategy where expressions are only evaluated when their results are needed.
Benefits:
Can work with infinite data structures
Improves performance for unused computations
Enables separation of concerns in code
Example:
haskell
Copy
-- Infinite list of Fibonacci numbers
fibs :: [Integer]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
Types:
Example:
haskell
Copy
-- Sum type
data Shape = Circle Float | Rectangle Float Float
-- Product type
data Person = Person { name :: String, age :: Int }
9. Type Classes
Definition:
A way to define generic interfaces that provide a common feature set for various types.
Eq
Ord
Show
Functor
Monad
Example:
haskell
Copy
class Sizeable a where
size :: a -> Int
10. Monads
Definition:
A design pattern that allows structuring programs generically while automating away
boilerplate code needed by the program logic.
Key Concepts:
Example:
haskell
Copy
-- Maybe monad for handling potential failure
safeDivide :: Float -> Float -> Maybe Float
safeDivide _ 0 = Nothing
safeDivide x y = Just (x / y)
Immutable
Often implemented using recursion
May use lazy evaluation
Common Structures:
Lists
Trees
Maps
Example:
haskell
Copy
-- Implementing a functional stack
type Stack a = [a]
Operator:
In Haskell: (.)
Example:
haskell
Copy
-- Function composition
isEvenLength :: [a] -> Bool
isEvenLength = even . length
-- Equivalent to:
-- isEvenLength xs = even (length xs)
Remember:
1. Immutability
Definition: Immutability refers to the property of an object whose state cannot be modified
after it is created. In functional programming, data structures are typically immutable.
Importance:
Predictability: Easier to reason about code behavior since immutable objects do not
change.
Functional Purity: Encourages the use of pure functions that rely solely on their
input parameters.
2. First-class Functions
Definition: Functions are treated as first-class citizens, meaning they can be assigned to
variables, passed as arguments, and returned from other functions.
Importance:
Flexibility: Allows for dynamic and flexible code structures, enabling patterns like
callbacks and function composition.
3. Pure Functions
Definition: A pure function is one that, given the same input, will always return the same
output and has no side effects (does not modify any external state).
Importance:
Testability: Easier to test since output is predictable and does not depend on
external state.
Debugging: Simplifies debugging as you can reason about the function's behavior
without considering external factors.
4. Referential Transparency
Importance:
Simplified Reasoning: Makes reasoning about code easier, leading to clearer and
more maintainable code.
Definition: A programming technique where expressions are not evaluated until their values
are needed, improving performance and resource management.
Importance:
Infinite Data Structures: Allows the creation and manipulation of infinite data
structures, as values are computed on demand.
6. Higher-order Functions
Definition: Functions that can take other functions as arguments or return them as results.
Importance:
7. Function Composition
Definition: The process of combining two or more functions to produce a new function.
Importance:
Summary