COSC 4 P 41 Functional Programming COSC 4

  • Slides: 19
Download presentation
COSC 4 P 41 – Functional Programming COSC 4 P 41 Functional Programming •

COSC 4 P 41 – Functional Programming COSC 4 P 41 Functional Programming • Instructor: Michael Winter – Office J 323 – Office Hours: Tue & Wed 10: 00 am - 12: 00 pm – email: mwinter@brocku. ca • Webpage: http: //www. cosc. brocku. ca/Offerings/4 P 41 • Course Description (Brock Calendar): Introduction to functional programming using the languages Haskell. Topics include all data types, type inference, pattern-matching, recursion, polymorphism, higher-order functions, lazy vs eager evaluation, modules and monads. • Prerequisites: None • Haskell: http: //www. haskell. org • course procedures – plagiarism © M. Winter 1. 1

COSC 4 P 41 – Functional Programming Textbooks • Main Text – Haskell, The

COSC 4 P 41 – Functional Programming Textbooks • Main Text – Haskell, The Craft of Functional Programming, 2 nd edition, S. Thompson, Addison - Wesley (1999), ISBN 0 -201 -34275 -8 • Supplemental Texts – The Hugs 98 User Manual, Mark P Jones, Alastair Reid, online documentation. – Real World Haskell, Bryan O'Sullivan, John Goerzen, Don Steward, O'Reilly (2009), ISBN 978 -0 -596 -51498 -3 – The Haskell School of Expression, P. Hudak, Cambridge University Press (2000), ISBN 0 -521 -64408 -9 © M. Winter 1. 2

COSC 4 P 41 – Functional Programming Course Work • Marking Scheme – Programming

COSC 4 P 41 – Functional Programming Course Work • Marking Scheme – Programming Assignments (2 x 10%, 2 x 20%) 60% – Final Exam (oral) 40% • Programming Assignments Number Due Late 1 Sep 28 @ 1: 00 pm Sep 30 @ 1: 00 pm 2 Oct 19 @ 1: 00 pm Oct 21 @ 1: 00 pm 3 Nov 09 @ 1: 00 pm Nov 11 @ 1: 00 pm 4 Nov 30 @ 1: 00 pm Dec 02 @ 1: 00 pm • Registration for final exam: Mid of October (Office hours or after class) • Final Exam (oral, 30 min each): Monday, December 07, 2009 © M. Winter 1. 3

COSC 4 P 41 – Functional Programming • • • Assignments will be available

COSC 4 P 41 – Functional Programming • • • Assignments will be available online. Submission is electronically. Details see webpage. Assignments will be returned by email. Assignments are due at the times specified above and will be accepted late until the indicated time, subject to a penalty of 25%. After the late period, assignments will not be accepted. A mark of at least 40% on the final exam is required to achieve a passing grade in this course. Assignments will be carefully examined regarding plagiarism. Cases of suspected plagiarism will be dealt with according to the University regulations and Departmental procedures. Consideration regarding illness for assignment submission or test dates will only be considered if accompanied with the completed Departmental Medical Excuse form. © M. Winter 1. 4

COSC 4 P 41 – Functional Programming Course Outline Week Date Book/Chapt. Topics 1

COSC 4 P 41 – Functional Programming Course Outline Week Date Book/Chapt. Topics 1 Sep 14/16 [1] 1– 3 Introduction to Functional Programming 2 Sep 21/23 [1] 4– 5 Recursion and Data Types 3 Sep 28/30 [1] 6– 7 Lists 4 Oct 05/07 [1] 9 -10 Patterns of Computation, Functions as Values 5 Oct 14/19* [1] 12 -13 Overloading, Type Classes, Type Checking 6 Oct 21/26 [1] 14 -15 Algebraic Types 7 Oct 28/Nov 02 [1] 16 Abstract Data Types 8 Nov 04/09 [1] 17 Lazy Evaluation 9 Nov 11/16 [1] 18 Programming with Actions 10 Nov 18/23 [1] 8, 14. 7 & 17. 9 Reasoning about Programs 11 Nov 25/30 [1] 8, 14. 7 & 17. 9 Reasoning about Programs II 12 Dec 02/03 [2] 7 Hugs – Language extensions, Review *October 12 is Thanksgiving, no classes. Make up on December 03. © M. Winter 1. 5

COSC 4 P 41 – Functional Programming Imperative languages • • • Von Neumann

COSC 4 P 41 – Functional Programming Imperative languages • • • Von Neumann model: – store with addressable locations machine code: – effect achieved by changing contents of store locations – instructions executed in sequence, flow of control altered by jumps imperative language: – variable corresponds to store location – instructions executed in sequence, flow of control altered by conditional and loop statements – efficient implementation since close to design of conventional computers © M. Winter 1. 6

COSC 4 P 41 – Functional Programming Functional languages • • computational model: lambda

COSC 4 P 41 – Functional Programming Functional languages • • computational model: lambda calculus mathematical functions: domain, range functional languages achieve effect by applying functions functional vs. imperative languages – store location – assignment statement vs. application of a function (expressions) • side-effects • aliasing • referential transparency © M. Winter 1. 7

COSC 4 P 41 – Functional Programming Features of functional languages • • usually

COSC 4 P 41 – Functional Programming Features of functional languages • • usually strongly typed (modern languages) algebraic type definitions – mathematical based notation – no (implicit) pointers higher-order functions – can accept functions as parameters – can return functions as results recursion as a basic principle application of rewrite rule: – function call replaced by code body run-time overhead garbage collection slogan: define “what to do”, not “how to do” © M. Winter 1. 8

COSC 4 P 41 – Functional Programming What is a function? A function is

COSC 4 P 41 – Functional Programming What is a function? A function is something which produces an output value depending on the input value(s). 12 34 inputs + output 46 A type is a collection of values. Usually functions are considered to take values of specific types as input, and produce values of another type. Int + Int A functional program is basically a list of definitions of functions. © M. Winter 1. 9

COSC 4 P 41 – Functional Programming Definitions Haskell definitions are of the form:

COSC 4 P 41 – Functional Programming Definitions Haskell definitions are of the form: name : : type name = expression Examples: size : : Int size = (12+13)*4 square : : Int -> Int square n = n*n © M. Winter 1. 10

COSC 4 P 41 – Functional Programming {-############################# First. Script. hs Simon Thompson, June

COSC 4 P 41 – Functional Programming {-############################# First. Script. hs Simon Thompson, June 1998 The purpose of this script is - to illustrate some simple definitions over integers (Int); - to give a first example of a script. #############################-} -- The value size is an integer (Int), defined to be -- the sum of twelve and thirteen. size : : Int size = 12+13 -- The function to square an integer. square : : Int -> Int square n = n*n -- The function to double an integer. double : : Int -> Int double n = 2*n -- An example using double, square and size. example : : Int example = double (size - square (2+2)) © M. Winter 1. 11

COSC 4 P 41 – Functional Programming ############################## First. Literate. lhs Simon Thompson, June

COSC 4 P 41 – Functional Programming ############################## First. Literate. lhs Simon Thompson, June 1998 The purpose of this script is - to illustrate some simple definitions over integers (Int); - to give a first example of a literate script. ############################## The value size is an integer (Int), defined to be the sum of twelve and thirteen. > > size : : Int size = 12+13 The function to square an integer. > > square : : Int -> Int square n = n*n The function to double an integer. > > double : : Int -> Int double n = 2*n An example using double, square and size. > > example : : Int example = double (size - square (2+2)) © M. Winter 1. 12

COSC 4 P 41 – Functional Programming The Booleans • • type Bool operations

COSC 4 P 41 – Functional Programming The Booleans • • type Bool operations && and || or not ex. Or : : Bool -> Bool ex. Or x y = (x || y) && not (x && y) © M. Winter 1. 13

COSC 4 P 41 – Functional Programming The integers • • • type Int:

COSC 4 P 41 – Functional Programming The integers • • • type Int: range – 2147483648… 2147483647 type Integer: range unbounded operations © M. Winter + sum * product ^ raise to the power - difference div whole number division mod remainder absolute value negate change sign 1. 14

COSC 4 P 41 – Functional Programming Relational operators and overloading > greater than

COSC 4 P 41 – Functional Programming Relational operators and overloading > greater than >= greater than or equal to == equal to /= not equal to <= less than or equal to < less than (==) for integers and Booleans. This means that (==) will have the type Int -> Bool Indeed t -> Bool if the type t carries an equality. (==) : : Eq a => a -> Bool © M. Winter 1. 15

COSC 4 P 41 – Functional Programming The rational numbers • • type Rational

COSC 4 P 41 – Functional Programming The rational numbers • • type Rational (import Ratio) operations % Integer -> Rational numerator the numerator denominator the denominator from. Integer -> Rational and +, *, -, negate, abs © M. Winter 1. 16

COSC 4 P 41 – Functional Programming The characters • type Char ‘a’ ‘t’

COSC 4 P 41 – Functional Programming The characters • type Char ‘a’ ‘t’ ‘n’ ‘\’ ‘’’ ‘”’ ‘97’ © M. Winter tab newline backslash single quote double quote character with ASCII code 97, i. e. , ‘ 9’ 1. 17

COSC 4 P 41 – Functional Programming Layout mystery x = x*x +x +2

COSC 4 P 41 – Functional Programming Layout mystery x = x*x +x +2 next x = … fun v 1 v 2 … vn | g 1 = e 1 | g 2 = e 2 … | otherwise = er © M. Winter 1. 18

COSC 4 P 41 – Functional Programming Operators and Do-it-yourself operators (+) : :

COSC 4 P 41 – Functional Programming Operators and Do-it-yourself operators (+) : : Int -> Int (+) 2 3 = 2 + 3 2 `max` 3 = max 2 3 Operator symbols !, #, $, %, &, *, +, . , /, <, =, >, ? , , ^, |, : , -, ~ (&&&) : : Int -> Int x &&& y | x > y = y | otherwise = x © M. Winter 1. 19