Fundamentals of Functional Programming

Slides:



Advertisements
Similar presentations
1 Programming Languages and Paradigms Lisp Programming.
Advertisements

Lists Introduction to Computing Science and Programming I.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Fundamentals of Python: From First Programs Through Data Structures
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Operators, Functions and Modules1 Pattern Matching & Recursion.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Input, Output, and Processing
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Overview of the Haskell 98 Programming Language
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
CSI 3125, Preliminaries, page 1 Data Type, Variables.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Polymorphic Functions
dr Robert Kowalczyk WMiI UŁ
Types CSCE 314 Spring 2016.
Lesson #6 Modular Programming and Functions.
A bit of C programming Lecture 3 Uli Raich.
Theory of Computation Lecture 4: Programs and Computable Functions II
Line Continuation, Output Formatting, and Decision Structures
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
PROGRAMMING IN HASKELL
Functions and patterns
Representation, Syntax, Paradigms, Types
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Haskell.
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Installation and exercises
Intro to PHP & Variables
Arrays, For loop While loop Do while loop
Line Continuation, Output Formatting, and Decision Structures
PROGRAMMING IN HASKELL
Number and String Operations
Clojure to Haskell (It’s mostly syntax).
Fundamentals of Programming
Representation, Syntax, Paradigms, Types
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
Matlab tutorial course
Type & Typeclass Syntax in function
Coding Concepts (Data- Types)
Topics Designing a Program Input, Processing, and Output
Representation, Syntax, Paradigms, Types
Haskell Types, Classes, and Functions, Currying, and Polymorphism
RECURSION Haskell.
PROGRAMMING IN HASKELL
Representation, Syntax, Paradigms, Types
Fundamental Programming
Functions and patterns
Functions and patterns
Computer Science 312 Making choices Tail Recursion
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
PROGRAMMING IN HASKELL
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Fundamentals of Functional Programming

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: https://www.haskell.org/platform 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”

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

Function Composition Here it is in action in Haskell

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

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

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

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

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)

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) < > <= >= ==

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)

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

Conditional Statements Here is an example of a function that checks if a number is even

Create the following functions isOdd isPositive

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

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

Pattern Recognition Here is an example Acts as a recursive function For summing a number (and all numbers below it)

Create the following functions factorial fibonnaci

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

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))

Create the following functions productList findInList

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

Create the following functions doubleNums areNumsEven