Download presentation
Presentation is loading. Please wait.
1
Fundamentals of Functional Programming
2
Functional Programming
We need to get our programming language set up before we look at functional programming We’ll be using Haskell Download from here: Choose Full option Once set up, need to run GHCi to run Haskell scripts First go to folder, :cd “file_path” Then load script, :load “file_name.hs”
3
Functional Programming
Functional Programming is a paradigm A ‘way of thinking’, or ‘strict thought process’ Involves thinking of program execution in terms of running individual functions Functions are more mathematical in nature than Object-Orientated Programming Functional Programming has three properties: Immutable functions (once a function has been made, it can’t be changed) Stateless operation (functions do not make use of outside values) Higher-Order functions (functions are used as parameters)
4
Functional Programming
Functions can be thought of as two things: A process – transforms information from one form into another An object – something that can act on other functions/pieces of data Take, for example, a sharpener As a process – sharpen the pencil As an object – the sharpener’s size, weight, colour
5
The input set is the domain The output set is the codomain
For the following functions, identify what the function does Its rule 𝑓: 0, 1, 2, 3 → 0, 1, 2, 3, …, 24, 25, 26, 27 𝑓 0 =0 𝑓 1 =1 𝑓 2 =8 𝑓 3 =27 𝑔: 0, 1, 2, 3 →{0, 1, 2, 3, 4, 5, 6} 𝑔 0 =0 𝑔 1 =2 𝑔 2 =4 𝑔 3 =6 The input set is the domain The output set is the codomain
6
Here are some of the types we can use in Haskell
Function Types In Haskell, we don’t need to specify types of input/output However, it does help us specify what we’re expecting If a function f takes an input of type A, and returns something of type B, we can say the function does as follows: 𝑓∷𝐴 →𝐵 Here are some of the types we can use in Haskell Integer Float Double Char String Bool
7
Function Types Here is an example of a Haskell function that triples a single number We have to save it to a Haskell script file (.hs) and load it in GHCi before we can use it
8
Function Types With functions, the last type is the return type
The other types are inputs So we can create a function with multiple inputs really easily
9
Try creating Haskell functions for the following:
Finding the square of a number Finding the cube of a number Function to double a number Finding the difference between two numbers Finding the area of a triangle
10
Functions as Parameters
Functions are considered first class objects Can be used as parameters Can be returned by functions This function doubles the result of another function
11
Functions as Parameters
Note that brackets in function signatures are not needed They help us understand what the function should be doing Typically brackets represent a single value In the type, they represent a single function They are needed in calculations when a single value is expected
12
Functions as Return Values
As mentioned earlier, functions can also be returned This function takes an integer, and returns an exponentiation function based off that integer
13
Functions as Return Values
That bit at the end is an anonymous function Before the arrow are the parameters After the arrow is the return value So we’re creating a function in a function And returning it
14
Partial Function Application
We can also give a function most of its parameters This won’t break We just need to store the result in another function When partially applying a function, we’re actually making another function With some of the values set We can finish the function by adding the remaining parameters
15
Partial Function Application
Here’s an example of it in action We can use let in GHCi to temporarily make a function The double function is made from partially applying mult
16
Function Composition Finally, we can also compose one function from two Involves running one function Then taking return and passing it into another function Often seen in maths as 𝑔∘𝑓 𝑥 , which means 𝑔 𝑓 𝑥 In Haskell, can use composition operator, or run functions in specific order
17
Function Composition Here it is in action in Haskell
18
Interesting GHCi Prelude Functions
The Prelude is Haskell’s base function library Where types like Integer, and functions like square-root come from Here’s a list of some of them Give them a try yourself sqrt 3 div 5 2 mod 5 2 cos 0 sin (0.5 * pi) gcd 45 54 lcm 4 5 take 5 “Hello World” drop 6 “Hello World” splitAt 6 “Hello World” words “The black cat sat on the mat!” (^) 2 4
19
Haskell Types: Characters
Characters are represented using single quotes Example: ‘a’ We can also use the actual ASCII code for the base ASCII table Between 0 and 127 Example: ‘\97’ Can make individual characters upper/lower case Using toUpper and toLower Can also check using isUpper and isLower
20
Haskell Types: Numbers
There are lots of different number types (under the Number umbrella) in Haskell Int: a whole number with 30 bits of precision Integer: a whole number with any number of bits of precision Float: single-precision floating point number Double: double-precision floating point number Rational: fractional type with no rounding error
21
Haskell Types: Numbers
The Number library offers quite a few functions we can use Mostly to do with rounding truncate 45.34 round 45.6 ceiling 5.3 floor 5.3 max 5 8 min 6 4
22
Haskell Types: Strings
Strings are literally a list of Char values Referred to as [Char] String values need to be enclosed in double-quotes Can concatenate Strings using the ++ operator “Functional” ++ “Programming” ++ actually appends lists (can be used with other data types)
23
AND (&&) OR (||) NOT (not) < > <= >= ==
Haskell Types: Bool Represents true and false values Bool values use a capital first letter True and False Haskell supports the usual operators (logical and relational) AND (&&) OR (||) NOT (not) < > <= >= ==
24
Anonymous Functions (\x -> x ^ 2)
We saw this earlier when making a function that makes other functions Called anonymous functions Aren’t given a name Are usually Assigned to another function Run in GHCi (\x -> x ^ 2)
25
Conditional Statements
Everything we’ve looked at so far as been sequential Running instructions one after the other However, Haskell does have another construct we can use Branching Can use it during the value we’re trying to return
26
Conditional Statements
Here is an example of a function that checks if a number is even
27
Create the following functions
isOdd isPositive
28
Pattern Recognition There’s one really important thing to talk about in Haskell There is no iteration That means no while-loops or for-loops Instead, functions have to make use of recursion to do anything To do this, we need to include termination conditions Could be done using if/else statements Easier done using pattern recognition
29
Pattern Recognition With this, we can state multiple possible values for a function to return From top-to-bottom The function will only return one of them Like a switch-statement If we want to return something first before other possibilities, we need to place it higher up in the function’s implementation
30
Pattern Recognition Here is an example Acts as a recursive function
For summing a number (and all numbers below it)
31
Create the following functions
factorial fibonnaci
32
Using Lists There are no arrays in Haskell
Only lists Lists have quite a few functions we can run on them head: returns the first element tail: returns all elements after the first init: returns all elements before the last last: returns the last element
33
Using Lists Here’s an example of summing all the numbers in a list
sumList :: [Integer] -> Integer sumList nums = if (null nums) then 0 else (head nums) + (sumList (tail nums))
34
Create the following functions
productList findInList
35
Using Map squareNums :: [Integer] -> [Integer]
We can also use lists in functions Best example is the map function Applies a function to values in a list Returns a list with the return value stored in each position squareNums :: [Integer] -> [Integer] squareNums nums = map (\x -> x ^ 2) nums
36
Create the following functions
doubleNums areNumsEven
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.