Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Cse536 Functional Programming Lecture #1, September 27, 2004 •Course Mechanics •Text Book •Down-loading Haskell •Syllabus - Course Overview •Functional Prog. Characteristics •The Hugs Interpreter •The Standard functions (the prelude) •Functions and operators •Defining Functions •Multiple Argument functions •Pattern Matching •Recursion and Induction •Layout rules •Typing and function prototypes •Ways to Create Functions 5/24/2017 1 Cse536 Functional Programming Abnormal Class Schedules Sun Mon Oct 24 25 Tue 26 Guest Wed 27 Thu 28 No Class 5/24/2017 Nov 1 2 3 Guest Lecture Midterm exam 29 Sat 30 Makeup class lecture 31 Fri 4 5 6 2 Cse536 Functional Programming Thanksgiving Class Schedule Sun Mon Nov 14 15 Tue Wed Thu Fri Sat 16 17 18 19 20 23 24 25 26 27 Lect 15 21 22 Postponed til Dec 6 28 29 5/24/2017 6 Lect 19 thanksgiving 30 Dec 1 Lect 18 2 3 4 7 8 9 10 11 Lect 17 5 cancelled Final Exam Finals week 3 Cse536 Functional Programming Acknowledgements The material taught in this course was made possible by many people. Here is a partial list: • Yale: Paul Hudak, John Peterson, Zhong Shao • Utrecht:, Daan Leijen • Mircosoft: Simon Peyton Jones, Erik Meijer, Conal Elliot • And my colleagues at OGI – – – – 5/24/2017 Jim Hook John Launchbury Andrew Tolmach (PSU) Dick Kieburtz 4 Cse536 Functional Programming Class Web Page • The CSE536 class web page can be found at: – www.cs.pdx.edu/~sheard/course/funprog • Contents of the page – – – – 5/24/2017 Course Syllabus Link to the Haskell home page Copies of the powerpoint slides used in lectures Copies of the Haskell code illustrated in the lectures 5 Cse536 Functional Programming Today’s Assignments Reading • The Haskell School of Expression Available In the OGI online bookstore – Chapter 1, pp 1-20 Search • Find the class webpage Programming Assignment – Due Wednesday • Login to some Haskell system, preferably Hugs. See how the system operates. Type in solutions to programming problems (In Class exercises 1 and 2 in this handout), get them running, print them out then turn them in on Wednesday. What matters here is that you try out a Haskell system, not that you get them perfect. Homework • Homework 1 is technically assigned on Wednesday, you can get a preview now on the class web page. Due October 6. 5/24/2017 6 Cse536 Functional Programming Course Information • CSE536 - Functional Programming – – – – – – Time: Monday & Wednesday 2:00-3:20 pm Place: Bronson Creek building classroom Instructor: Tim Sheard office: room 115, CS Dept, 4th Ave Building, Portland State Univ. phone: 503-725-2410 (work) 503-649-7242 (home) office hours: After class, or by Appt. • Assignments – weekly programming assignments • Grading: – 1 midterm exam (30%) – 1 final exam, or project (to be decided later) (30%) – Weekly assignments (40%) 5/24/2017 7 Cse536 Functional Programming Text Book • Text: The Haskell School of Expression - by Paul Hudak • Other Reference Materials – Auxillary Material » Haskell: The Craft of Functional Programming by Simon Thompson, Addison Wesley, ISBN 0-201-40357-9 » Elements of Functional Programming by Chris Reade, Addison Wesley, ISBN 0-201-12915-9 » Intro to Functional Programming (Currently out of print) by Richard Bird & Phillip Wadler. Prentice Hall, ISBN 0-13-484189-1 • Class Handouts – Each class, a copy of that day’s slides will be available as a handout. – I will post files that contain the example programs used in each lecture on the class web page www.cs.pdx.edu/~sheard/course/funprog – www.cse.ogi.edu/~sheard/cse502f2002/ (An older version) – I will post Assignments there as well. 5/24/2017 8 Cse536 Functional Programming Academic Integrity Students are expected to be honest in their academic dealings. Dishonesty is dealt with severely. • Homework. Pass in only your own work. • Program assignments. Program independently. • Examinations. Notes and such, only as each instructor allows. OK to discuss how to solve problems with other students, but each student should write up, debug, and turn in his own solution. 5/24/2017 9 Cse536 Functional Programming Haskell • In this course we will use an implementation of the functional language Haskell: Hugs • The Haskell Homepage has lots of useful information: http://www.haskell.org/ • Other implementations (which we won’t use) include: – GHC Glasgow Haskell Compiler http://www.dcs.gla.ac.uk/fp/software/ghc/ – HBC - Swedish compiler http://www.cs.chalmers.se/~augustss/hbc/hbc.html • Hugs for windows95 download from: http://www.haskell.org/hugs/ Be sure and use the latest version of Hugs. Earlier versions do not have the correct versions of the graphics libraries in the standard distribution. This version works "out of the box". 5/24/2017 10 Cse536 Functional Programming Course Thesis • This course is about learning to program in functional languages. • Good functional programs have a style unlike programs in more traditional languages. They are higher level, more concise, and more amenable to formal manipulation. We will learn to take advantage of these properties. • In order to do this, there will be extensive programming assignments in Hugs, a Lazy functional language, based upon the Haskell programming language. – If you don’t do - you won’t learn – You’re deluding yourself if you think you can learn the material without doing the exercises! 5/24/2017 11 Cse536 Functional Programming Functional Programming Characteristics • Applicative style – input output description of problem. • First class functions – pass as parameters – return as value of a function – store in data-structures • Type system separates computation from action (side effects) – Programs which make assignments or have side effects, can be immediately determined by inspecting their type. • Use of formal methods to transform one program into another, using the rule of substitution of equals. • Less Importantly: – Automatic memory management (G.C. no new or malloc) – Use of a strong type system which uses type inference, i.e. no declarations but still strongly typed. 5/24/2017 12 Cse536 Functional Programming Functional Languages • Functional languages have had serious drawbacks, especially in the areas of : – Performance – Interoperability with existing (legacy or commercially available) components. • Removing these barriers has been a major thrust of recent research . Most of these perceptions no longer hold true. 5/24/2017 13 Cse536 Functional Programming The Hugs Interpreter Start Hugs Demo __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2003 ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Report bugs to: [email protected] || || Version: Nov 2003 _________________________________________ Hugs mode: Restart with command line option +98 for Haskell 98 mode Hugs session for: C:\Program Files\Hugs98/libraries\Hugs\Prelude.hs C:\Program Files\Hugs98/libraries\Prelude.hs D:\work\sheard\Courses\CSE502\Fall2004\lect01.html Type :? for help Main> 5/24/2017 14 Cse536 Functional Programming Hugs Help- : <command> Main> :? LIST OF COMMANDS: Any command may be abbreviated to :c where c is the first character in the full name. :load <filenames> :load :also <filenames> :reload :edit <filename> :edit :module <module> <expr> :type <expr> :? :set <options> :set :names [pat] :info <names> :browse <modules> :find <name> :!command :cd dir :gc :version :quit Main> 5/24/2017 load modules from specified files clear all files except prelude read additional modules repeat last load command edit file edit last module set module for evaluating expressions evaluate expression print type of expression display this list of commands set command line options help on command line options list names currently in scope describe named objects browse names exported by <modules> edit module containing definition of name shell escape change directory force garbage collection print Hugs version exit Hugs interpreter 15 Cse536 Functional Programming Setting the search path • When hugs looks for libraries and modules, it looks in the directories denoted by the search path. • :set – will display the path (and lots of other things) • :set -P;c:\xxx;d:\yyy – will add c:\xxx and d:\yyy to the existing path (indicated by the leading semicolon) 5/24/2017 16 Cse536 Functional Programming Hugs use ? :cd /ogi/staff/sheard/cse502 ? 5+2 7 ? 5 * 2 + 3 13 ? sqrt 4.0 2.0 ? sum [2,3,4] 9 ? length [2,3,4,5] 4 ? sort [3,4,1,2,77,6] [1, 2, 3, 4, 6, 77] ? 5/24/2017 17 Cse536 Functional Programming Hugs use - typing expressions Main> Main> :t sort sort :: Ord a => [a] -> [a] Main> :t mymap mymap :: (a -> b) -> [a] -> [b] Main> :t mymap (\x -> x+1) mymap (\x -> x + 1) :: Num a => [a] -> [a] Main> 5/24/2017 18 Cse536 Functional Programming Syntactic Elements • Identifiers start with a lower case letter followed by digits or other letters or primes – Valid Examples: a a3 a’b aF – Invalid Examples: F1 Good • Reserved words – – – – 5/24/2017 case class data else if in infix infixl infixr instance let of primitive then type where 19 Cse536 Functional Programming Syntactic Elements (cont.) • Constructors – Constructors start with an Uppercase letter – Primitive operator constructors [] and : • Operators: Combinations of – :# $ % & * + - = . / \ < > ? ! @ ^ | – Reserved operators » :: = .. -- @ \ | <- -> ~ => • Operators are used in an “infix” manner – 5+3 – (f x) ==> (\ x -> 3 + 4) – Any identifier can be made infix by using backquotes. » E.g. 10 `in` w and 3 `choose` 5 » GITH pg 12, FN pg 21 5/24/2017 20 Cse536 Functional Programming Functions • Functions are defined in Files and loaded into to Hugs. Example: – :l lect01.hs • Functions on numbers – Type of numbers: Int and Float – Conversion functions: fromInteger round • Functions on Booleans – Relational operators < > <= >= – Combinators && || not == /= • Examples ? 5 > 7 False ? 1==4 False 5/24/2017 21 Cse536 Functional Programming Lists in Hugs ? [1,2] ++ [4] [1, 2, 4] Lists are constructed with brackets. ++ is list concatenation ? null [2] False ? take 3 [1,2,3,4,5,6] [1, 2, 3] ? drop 3 [1,2,3,4,5,6] [4, 5, 6] 5/24/2017 22 Cse536 Functional Programming Constructing Lists • The Empty List [] • The "Cons" ( : ) Constructor ? 3 : [3,4,5] [3, 3, 4, 5] • The Dot Dot notation ? [1 .. 4] [1, 2, 3, 4] • The Comprehension Notation ? [x + 1 | x <- [2..4]] [3, 4, 5] ? [ (x,y) | x <- [1..2], y <- [3,5,7]] [(1,3), (1,5), (1,7), (2,3), (2,5), (2,7)] ? [ x * 2 | x <- [1..10], even x] [4, 8, 12, 16, 20] 5/24/2017 23 Cse536 Functional Programming Taking Lists Apart ? head [1,2,3] 1 ? tail [1,2,3] [2, 3] ? take 2 [1,2,3] [1,2] ? drop 2 [1,2,3] [3] 5/24/2017 24 Cse536 Functional Programming Function Definitions • Functions defined in files by writing equations visit a sample file.hs • Example: plusone x = x + 1 • Sample Output when loading: Hugs session for: E:\programs\Hugs98Dec01\\lib\Prelude.hs E:\work\sheard\Courses\CSE502\web\lecturenotes\lect01.hs Type :? for help Main> ? plusone plusone ? plusone 4 5 5/24/2017 25 Cse536 Functional Programming In Class Exercise 1 • Define prefix and lastone in terms of head tail and reverse. First make a file “lect01.hs” lastone x = head (reverse x) prefix x = reverse (tail (reverse x)) • Sample Hugs run ? :l lect01.hs Reading script file "lect01.hs": Hugs session for: E:\work\sheard\Courses\CSE502\web\lecturenotes\lect01.hs ? lastone [1,2,3,4] 4 ? prefix [1,2,3,4] [1, 2, 3] ? 5/24/2017 26 Cse536 Functional Programming Functions with Multiple Arguments • Example Definitions difference x y = x - y choose x y z = if x then y else z • Example Session: Hugs session for: E:. . .\lect01.hs ? :type difference difference :: Int -> Int -> Int ? :type choose choose :: Bool -> a -> a -> a ? • Arrow is right Associative a -> b -> c = 5/24/2017 a -> (b -> c) 27 Cse536 Functional Programming In Class Exercise 2 • define map and filter functions – – map f [1,2,3] = [f 1, f 2, f 3] filter even [1,2,3,4,5] = [2,4] mymap f l = [ f x | x <- l] myfilter p l = [ x | x <- l, p x] • Sample Session ? :r Reading script file "lect01.hs": Hugs session for: E:\work\sheard\Courses\CSE502\web\lecturenotes\lect01.hs ? mymap plusone [2,3,4] [3, 4, 5] ? myfilter even [1,2,3,4,5,6] [2, 4, 6] 5/24/2017 28 Cse536 Functional Programming Types of Errors • Syntax errors (use of symbols in unexpected places) f x = (x*0) = x ? :r Reading script file "lect01.hs": Parsing ERROR "lect01.hs" (line 36): Syntax error in input (unexpected `=') ? • Undefined variables lastone2 = head . rev ? :r Reading script file "lect01.hs": Dependency analysis ERROR "lect01.hs" (line 38): Undefined variable "rev" ? 5/24/2017 29 Cse536 Functional Programming Types of Errors (cont.) • Type errors ? :type hd hd :: [a] -> a ? :type plusone plusone :: Int -> Int ? hd 3 ERROR: Type error in *** expression : *** term : *** type : *** does not match : application hd 3 3 Int [a] ? plusone [1,2] ERROR: Type error in *** expression : *** term : *** type : *** does not match : application plusone [1,2] [1,2] [Int] Int 5/24/2017 30