Download Functional Programming Course (CS457/557) at Portland State University - Prof. Timothy E. and more Exams Computer Science in PDF only on Docsity!
CS 457/557 Functional Programming
Lecture 1
Course Overview and Introduction
Course Information
• CS457/557 Functional Programming
- Tuesday & Thursday 2:003: 30 pm
- NH 341
- Guest Instructor: Mark Jones
- Regular Instructor (starting with 4 th
lecture): Andrew Tolmach
- Phone: 7255492
- Email: [email protected]
- Office hours: TuTh 45 or by appt.
- Web page: http://www.cs.pdx.edu/~apt/cs
• Assignments:
- Weekly programming assignments, due Tuesdays (40%)
• Exams:
- Midterm exam (30%); Final exam (30%)
What does “functional” mean?
• Programs consist of functions with no sideeffects
- “Applicative” style
- Input/output description of problem
- Build programs by function composition
- No accidental coupling between components
- Flexible evaluation order
• Functions are “first class” values
- Pass as parameters
- Return as value of a function
- Store in datastructures
- Supports higherlevel, “declarative” programming style
Functional Languages
• Applicative style
- Encouraged or required, depending on language.
• Firstclass functions
• Emphasis on types
- Builtin support for lists and other recursive data types
- Type inference = strong static type checking but no
declarations needed
- Type system separates pure computations from actions
(computations with side effects)
• Automatic memory management
- Garbage collection; no new or malloc
• Emphasis on (informal) program proof
- Easy laws for program transformation
Haskell
• Developed by committee in late 1980’s
- Combined and standardized several earlier languages.
- Now dominant “lazy” pure FP language.
- Current stable version is “Haskell 98”
- Many experimental extensions available.
• We will use an interpreter called Hugs.
- Available for most platforms
- Installed on PSU Solaris network (package hugs)
- Easy to download to your PC (get Hugs98, November
version)
• There are also other interpreters, compilers.
• The Haskell homepage has lots of useful information:
Simple expressions in Hugs
Prelude> 5+ 7 Prelude> 5 * 2 + 3 13 Prelude> sqrt 4.
Prelude> sum [2,3,4] 9 Prelude> length [2,3,4,5] 4 Prelude> sort [3,4,1,2,77,6] [1, 2, 3, 4, 6, 77] Prelude>
Syntactic Elements (cont.)
• Operators
- Formed by combinations of »!^ #^ $^ %^ &^ * +^.^ /^ <^ =^ >^?^ @^ ^ ^^ |^ -^ ~ :
- Excluding certain reserved sequences: » .. :: = \ | <- -> @ ~ =>
- Used in an “infix” manner: » E.g. 5 + 3
- Can be made “prefix” by enclosing in parentheses » E.g. (+) 5 3
- Any identifier can be made infix by using backquotes. » (^) E.g. 10
in w or 3 choose 5
• Literals
- Integers, e.g. 123 39949993 0xff7f 0o
- Floating point, e.g. 3.14 7.0 0.45 8.5e
- Characters,e.g. ’a’ ’Z’ ’\n’ Strings, e.g. “abc” “def\n”
Functions
• Functions are defined by equations in files
• Example file lect01.hs:
plusone :: Int -> Int
plusone x = x + 1
• Example dialog in hugs:
Prelude> :l lect01.hs
Reading file “lect01.hs”:
Hugs session for:
C:\hugs\lib\Prelude.hs
lect01.hs
Main> plusone 41
Constructing Lists
• The Empty List []
• The "Cons" ( : ) Constructor
Prelude> 3 : [3,4,5] [3, 3, 4, 5]
• The Dot Dot notation
Prelude> [1 .. 4] [1, 2, 3, 4]
• The Comprehension notation
Prelude> [x + 1 | x <- [2..4]] [3, 4, 5] Prelude> [ (x,y) | x <- [1..2], y <- [3,5,7]] [(1,3), (1,5), (1,7), (2,3), (2,5), (2,7)] Prelude> [ x * 2 | x <- [1..10], even x] [4, 8, 12, 16, 20]
Taking Lists Apart
Prelude> head [1,2,3] 1 Prelude> tail [1,2,3] [2, 3] Prelude> null [2] False Prelude> take 2 [1,2,3] [1,2] Prelude> drop 2 [1,2,3] [3]
Thinking about Functions
• Can picture function as a box with some inputs and an
output:
difference
reverse head
[1,2,3]
[3,2,1]
lastone
Thinking about Types
• A type is a collection of values. Functions can only be
applied to arguments of appropriate types.
difference
Int
reverse head
[Int]
Int
[Int]
lastone
Int
Int