© m. winter cosc 4p41 – functional programming 1.11.1 cosc 4p41 functional programming...
TRANSCRIPT
© M. Winter
COSC 4P41 – Functional Programming
1.1
COSC 4P41Functional Programming
• Instructor: Michael Winter– Office J323– Office Hours: Wed & Fri 1:00pm – 3:00pm– email: [email protected]
• Webpage: http://www.cosc.brocku.ca/~mwinter/Courses/4P41/
• 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: three and one-half COSC credits or permission of the instructor.
• Haskell:http://www.haskell.org
© M. Winter
COSC 4P41 – Functional Programming
1.2
Textbooks
• Main Text– Haskell, The Craft of Functional Programming, 3rd
edition, S. Thompson, Addison - Wesley (2011), ISBN 0-201-88295-7
• Supplemental Texts– The Haskell 2010 Report, Simon Marlow, online. – 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
COSC 4P41 – Functional Programming
1.3
Course Work
• Marking Scheme– Lab Tests (3x20%) 60%– Final Lab Exam 40%
• Tests & Exam (D205) Lab Test 1 January 31 @ 3:30pm – 5:00pm Lab Test 2 February 28 @ 3:30pm – 5:00pm Lab Test 3 March 21 @ 3:30pm-5:00pm Exam April 07 @ tba
© M. Winter
COSC 4P41 – Functional Programming
1.4
• A mark of at least 40% on the final exam is required to achieve a passing grade in this course.
• Consideration regarding illness for test dates will only be considered if accompanied with the completed Departmental Medical Excuse form.
• Academic misconduct is a serious offence. The principle of academic integrity, particularly of doing one's own work, documenting properly (including use of quotation marks, appropriate paraphrasing and referencing/citation), collaborating appropriately, and avoiding misrepresentation, is a core principle in university study. Students should consult Section VII, 'Academic Misconduct", in the "Academic Regulations and University Polices“ entry in the Undergraduate Calendar, available at brocku.ca/webcal to view a fuller description of prohibited actions, and the procedures and penalties.
© M. Winter
COSC 4P41 – Functional Programming
1.5
Course OutlineWeek
Date Book/Chapt. Topics
1 Jan 08/10 [1] 1–3 Introduction to Functional Programming
2 Jan 15/17 [1] 4–5 Recursion and Data Types
3 Jan 22/24 [1] 6–7 Lists
4 Jan 29/31 [1] 9-10 Patterns of Computation, Functions as Values
5 Feb 05/07 [1] 12-13 Overloading, Type Classes, Type Checking
6 Feb 12/14 [1] 14-15 Algebraic Types
7 Feb 26/28*
[1] 16 Abstract Data Types
8 Mar 05/07 [1] 17 Lazy Evaluation
9 Mar 12/14 [1] 18 Programming with Actions
10 Mar 19/21 [1] 8, 14.7 & 17.9
Reasoning about Programs
11 Mar 26/28 [1] 8, 14.7 & 17.9
Reasoning about Programs II
12 Apr 02/04 [2] 7 Language extensions, Review
*Feb 17-21 is Reading Week.
© M. Winter
COSC 4P41 – Functional Programming
1.6
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
COSC 4P41 – Functional Programming
1.7
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
COSC 4P41 – Functional Programming
1.8
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
COSC 4P41 – Functional Programming
1.9
What is a function?
+1234 46
inputsoutput
A functional program is basically a list of definitions of functions.
+Int
IntInt
A function is something which produces an output value depending on the input value(s).
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.
© M. Winter
COSC 4P41 – Functional Programming
1.10
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
COSC 4P41 – Functional Programming
1.11
{-#########################################################
FirstScript.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
COSC 4P41 – Functional Programming
1.12
###########################################################
FirstLiterate.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
COSC 4P41 – Functional Programming
1.13
The Booleans
• type Bool• operations
exOr :: Bool -> Bool -> Bool
exOr x y = (x || y) && not (x && y)
&& and
|| or
not not
© M. Winter
COSC 4P41 – Functional Programming
1.14
The integers
• type Int: range –2147483648…2147483647• type Integer: range unbounded• operations + sum
* product
^ raise to the power
- difference
div whole number division
mod remainder
abs absolute value
negate change sign
© M. Winter
COSC 4P41 – Functional Programming
1.15
Relational operators and overloading
(==) for integers and Booleans. This means that (==) will have the type
Int -> Int -> Bool
Bool -> Bool -> Bool
Indeed t -> t -> Bool if the type t carries an equality.
(==) :: Eq a => a -> a -> Bool
> greater than
>= greater than or equal to
== equal to
/= not equal to
<= less than or equal to
< less than
© M. Winter
COSC 4P41 – Functional Programming
1.16
The rational numbers
• type Rational (import Ratio)• operations
and +, *, -, negate, abs
% Integer -> Integer -> Rational
numerator the numerator
denominator the denominator
fromInteger Integer -> Rational
© M. Winter
COSC 4P41 – Functional Programming
1.17
The characters
• type Char
‘a’
‘\t’ tab‘\n’ newline‘\\’ backslash‘\’’ single quote‘\”’ double quote‘\97’ character with ASCII code 97, i.e., ‘9’
© M. Winter
COSC 4P41 – Functional Programming
1.18
Layout
mystery x = x*x
+x
+2
next x = …
fun v1 v2 … vn
| g1 = e1
| g2 = e2
…
| otherwise = er
© M. Winter
COSC 4P41 – Functional Programming
1.19
Operators and Do-it-yourself operators
(+) :: Int -> Int -> Int
(+) 2 3 = 2 + 3
2 `max` 3 = max 2 3
Operator symbols !,#,$,%,&,*,+,.,/,<,=,>,?,\,^,|,:,-,~
(&&&) :: Int -> Int -> Int
x &&& y
| x > y = y
| otherwise = x