Upload
gail-mullen
View
14
Download
1
Embed Size (px)
DESCRIPTION
Functional Programming Lecture 1 - Introduction. Professor Muffy Calder. About the Course. the Haskell functional language Lectures + Laboratories + Problem Sessions 2 Assignments Web site Lecture notes Assignments and problem sheets Resources Textbook - PowerPoint PPT Presentation
Citation preview
Functional ProgrammingLecture 1 - Introduction
Professor Muffy Calder
About the Course
• the Haskell functional language
• Lectures + Laboratories + Problem Sessions
• 2 Assignments
• Web site– Lecture notes
– Assignments and problem sheets
– Resources
• Textbook
Functional Programming in Haskell
by Simon Thompson
Programming Languages
• imperative: Ada, C, Pascal, …
• object: Java, C++, Smalltalk …
• logic: Prolog, ...
• functional: Haskell, ML, Lisp, Scheme, …– Haskell named after Haskell B. Curry
– Not Schoenfinkel
– Haskell is lazy
– ML is strict
What is Functional Programming?
• Based on mathematical functions
• Example– A function to compute whether or not an integer is in a list
of integers
inlist :: Int -> [Int] -> Bool
inlist x [] = False
inlist x (y:ys) = | (x == y) = True
| otherwise = inlist x ys
inlist 3 [5,3,6]
inlist 2 [5,3,6]
• Key concepts– Types
– Recursion
Why use Functional Languages?
• Concise and powerful style
• Rapid prototyping
• Strong error checking by intepreter/compiler
• Reduced debugging time
• Reliable, correct software
• Formal reasoning
• Why teach it in second year?– Good discipline + more abstract way of thinking
Haskell
• Standard functional language– developed by international committee
• 3 people from GU on that committee
• Used as the basis for numerous projects, in numerous countries
• Has technical characteristics that support software engineering and formal methods
Hugs
• An interactive interpreter for Haskell98– Hugs98 for windows
• Very quick ‘compilation’ and error checking
• Available on nearly all computers
• Free software
• See http://haskell.org/hugs
Overview of Functional Programming
2 slogans
• everything is a function
• every function has a type
FP consists of
• defining types
• defining functions
• applying functions and calculating the resulting expression
ftyped inputs
typedoutput
Types, Constants and Expressions
• type - set of possible valuesInt
• constant - a particular value in a type27:: Int
• expression - can be evaluated2+3*x
• notation: 2+2 => 4“2+2 evaluates to 4”
The type of + is Int->Int-Int, i.e._+_ :: Int->Int->Int
Int and Integer
• Int - 32-bit integers
• Integer - genuine integers
– no limit on size (except memory)
– all operations give correct result
– programmer doesn’t have to worry about how much memory to allocate
You will usually use Int.
Float and Double
• Single and double precision floating point numbers
• Does not give exact real arithmetic - there can be roundoff errors
• It is unlikely that you will use these types in this course.
Char
• ASCII characters
• constant - surround with single quote ‘x‘ :: Char
• can compare characters
`c` < ‘Z’
• case conversion
– toUpper ‘ w ‘ => ‘ W ‘
– toLower ‘Q ‘ => ‘ q ‘
– toUpper :: Char -> Char
Bool
• Boolean values, used to control conditionals
• only two constants: False :: Bool and
True :: Bool
• b && c (conjunction)
• b || c (disjunction)
• not b (negation)
Example:
( 3 <= x) && (x <= 10) :: Bool
Function application
• Write the name of the function followed by the arguments
• Sometimes use terminology rator and rand
• Don’t put parentheses around the argument
sqrt 9.0
fcn 2 8 13
3.4 + (sqrt x) * 100
2 * (sqrt (pi*r*r)) + y
E.g. In Maths f(x)
In FP (f x)