Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Modern Programming Languages, 2nd ed.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Cse536 Functional Programming 1 6/10/2015 Lecture #6, Oct 13, 2004 Reading Assignments –Read chapter 5 of the Text Polymorphic and Higher-Order Functions.
Cs536 Functional Programming 16/13/2015 Lecture #2.5 Today’s Topics A Few Less Trivial Examples – Numerical Functions »Differentiation and square root.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Advanced Programming Andrew Black and Tim Sheard Lecture 2 Intro to Haskell.
Cse536 Functional Programming 1 6/28/2015 Lecture #3, Oct 4, 2004 Reading Assignments –Finish chapter 2 and begin Reading chapter 3 of the Text Today’s.
Chapter 5 Polymorphic and Higher-Order Functions.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Creating Functions Functional Programming. The function calculator Functional programming is all about using functions Functions are first class – Take.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming Lecture 4 - More Lists Muffy Calder.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Lecture 14: Advanced Topic: Functional Programming
Polymorphic Functions
Functional Programming
Conditional Expressions
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Functions and patterns
A lightening tour in 45 minutes
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
CSE 341 Section 5 Winter 2018.
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
Functions and patterns
PROGRAMMING IN HASKELL
Functions and patterns
Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage, under the assignments link, and as an extra handout, given out in class today Today’s Topics –Definition by cases –Definition by patterns –Local definitions –Function types and prototyping –Overloaded functions –Ways to make functions –Tuples –Polymorphism –Operators as functions –Functions as Arguments –Functions returned as values

Cse536 Functional Programming 2 7/14/2015 Definition by Cases absolute x | x < 0 = -x | x >= 0 = x ? absolute 3 3 (6 reductions, 13 cells) ? absolute (- 5) 5 (9 reductions, 13 cells) ?

Cse536 Functional Programming 3 7/14/2015 Definition By Patterns Example on Booleans myand True False = False myand True True = True myand False False = False myand False True = False Order Matters –Variables in Patterns match anything myand2 True True = True myand2 x y = False –What happens if we reverse the order of the two equations above? Pattern may contain constructors constructors are always capitalized

Cse536 Functional Programming 4 7/14/2015 Patterns On lists File Contents hd (x:xs) = x tl (x:xs) = xs firstOf3 [x,y,z] = x Hugs Session ? hd [1,2,3] 1 ? firstOf3 [1,2,3] 1 ? firstOf3 [1,2,3,4] Program error: {firstOf3 [1, 2, 3, 4]}

Cse536 Functional Programming 5 7/14/2015 Rules for Patterns All the patterns (on the left) should have compatible types The cases should (but are not required to) be exhaustive There should be no ambiguity as to which case applies. Ordering fixes ambiguity if there is any. A Pattern is: –A variable x –A constructor applied to patterns x:xs or Branch(x,y,z) –A constant 3 or [] –A tuple of patterns (x,3,y:ys)

Cse536 Functional Programming 6 7/14/2015 Recursive Functions & Comments File Contents plus x y = if x == 0 then y else 1 + (plus (x-1) y) len [] = 0 len (x:xs) = 1 + (len xs) Hugs session ? plus ? len [1,2,3,4] 4

Cse536 Functional Programming 7 7/14/2015 Comments -- A comment to the end of the line {- This is a comment to the next -} {- Nesting of {- -} pairs allowed -}

Cse536 Functional Programming 8 7/14/2015 Local Definitions Local Definitions: Where len2 [] = 0 len2 (x:xs) = one + z where z = len2 xs one = 1 Indentation matters ! Same sequence of tokens but different meaning where a = f x y -- y is a variable used as b = g z -- an arg to function f where a = f x y b = g z -- y is the name of fun -- being defined Location of makes a big difference RULE of thumb Definitions at the same scope should be indented equally far.

Cse536 Functional Programming 9 7/14/2015 Function Types & Prototyping Typing f a b c = a + b + c + 1 has type f :: Int -> Int -> Int -> Int Read as f:: Int -> (Int -> (Int -> Int)) Prototyping plus :: Int -> Int -> Int plus x y = if x == 0 then y else 1 + (plus (x-1) y) myand :: Bool -> Bool -> Bool myand True False = False myand True True = True myand False False = False myand False True = False

Cse536 Functional Programming 10 7/14/2015 Overloading and Classes ? :type difference difference:: Num a => a -> a -> a ? difference 3 4 ? difference ? The class Num is a predicate on types

Cse536 Functional Programming 11 7/14/2015 Ways to Create Functions By defining: plusone x = x+1 ? plusone 3 4 By operator section ? (3+) 5 8 ? map (3+) [2,3,4] [5, 6, 7] By lambda expression ? (\ x -> x+2) 5 7 ? map (\x -> x*2) [2,3,4] [4, 6, 8]

Cse536 Functional Programming 12 7/14/2015 Creating Functions (cont.) By currying (partial application) ? plus 3 plus 3 ? :type (plus 3) plus 3 :: Int -> Int ? map (plus 3) [3,4] [6, 7] By composition ? map (head. tail) [[2,3,4],[4,5,6]] [3, 5] By combinator: k x y = x – Functions which return functions ? map (k 3) [1,2,3] [3, 3, 3]

Cse536 Functional Programming 13 7/14/2015 Expressions, Values, and Types There are three types of distinguished entities in Haskell Expressions –5 + 3 –len [1,2,3] –rev [2,9] –len Values –8 –3 –[9,2] – > Types –Int –[ Int ] –[ a ] -> Int

Cse536 Functional Programming 14 7/14/2015 Tuples Heterogeneous Collection of a fixed width Tuple Expressions –(5+3, not True) –(tl [1,2,3], [2]++[3,4], “abc”) Evaluate to Tuple Values –(8, False) –([2,3], [2,3,4], “abc”) And have Tuple Types –(Int, Bool) –([Int], [Int], String)

Cse536 Functional Programming 15 7/14/2015 Typing Tuples ? :type (1,"x",True) (1,"x",True)::(Int,String,Bool) ? :type (1,2) (1,2) :: (Int,Int) ? :type (2,("x",3)) (2,("x",3))::(Int,(String,Int)) –Note: (Int,([Char],Int)) <> (Int,[Char],Int) Pattern matching on tuples ? (\ (a,b) -> a) (2,3) 2 ? (\ (a,b) -> b + 2) (2,3) 5

Cse536 Functional Programming 16 7/14/2015 Used when returning multiple values Function that splits a list into two pieces at some particular position split 2 [1,2,3,4] --> ([1,2],[3,4]) split 3 [1,2,3,4,5,6,7] ---> ([1, 2, 3],[4, 5, 6, 7]) split 0 [1,2,3] --> ([],[1,2,3]) split 0 x = ([],x) split n [] = ([],[]) split n (x:xs) = (x:ys,zs) where (ys,zs) = split (n-1) xs

Cse536 Functional Programming 17 7/14/2015 Polymorphism (FN pp 16, GITH pp3-5) Consider: tag1 x = (1,x) ? :type tag1 tag1 :: a -> (Int,a) Other functions have types like this consider (++) ? :type (++) (++) :: [a] -> [a] -> [a] ? :type ([1,2]++) ([1,2] ++) :: [Int] -> [Int] What are some other polymorphic functions and their types? –id :: –reverse :: –head :: –tail :: –(:) :: –split ::

Cse536 Functional Programming 18 7/14/2015 Operators as functions (FN pp 6-8 & 21 GITH pp 11-12) ? :type (:) (:) :: a -> [a] -> [a] ? :type (++) (++) :: [a] -> [a] -> [a] ? :type (+) (+) :: Num a => a -> a -> a Operator Precedence –level 9. !! –level 8 ^ –level 7 * / `div` `rem` `mod` –level –level 5 : ++ \\ –level 4 == /= >= `elem` –level 3 && –level 2 || –level 1 (not used in the prelude)

Cse536 Functional Programming 19 7/14/2015 Associativity (FN pp 22-23) Right –2 : 3 : 4 : [] = 2 : (3 : (4 : [])) –Other Right Associative operators (:) (^) (++) (&&) (||) Left – = (7 - 6) - 2 –Other Left Associative operators (!!) (-) (*) (+) Non-associative –( )... (i.e. all the relational operators)

Cse536 Functional Programming 20 7/14/2015 Defining one’s own operators (FN pp 23) infix 4 `inlist` infix 3 -&- x `inlist` [] = False x `inlist` (y:ys) = if x==y then True else x `inlist` ys ? 3 `inlist` [1,2,4] False ? 3 `inlist` [1,2,3,5] True

Cse536 Functional Programming 21 7/14/2015 Functions as arguments (FN pp 25-28) Consider: mymap f [] = [] mymap f (x:xs) = (f x):(mymap f xs) The parameter f is a function! What is the type of mymap ? mymap :: (a -> b) -> [a] -> [b] What happens when it is applied? map add1 map ( \ x -> 3) [1,2,3]

Cse536 Functional Programming 22 7/14/2015 When do you define a higher order function? Abstraction is the key mysum [] = 0 mysum (x:xs) = (+) x (mysum xs) myprod [] = 1 myprod (x:xs) = (*) x (myprod xs) myand [] = True myand (x:xs) = (&&) x (myand xs) Note the similarities in definition and in use ? mysum [1,2,3] 6 ? myprod [2,3,4] 24 ? myand [True, False] False

Cse536 Functional Programming 23 7/14/2015 Abstracting myfoldr op e [] = e myfoldr op e (x:xs) = op x (myfoldr op e xs) ? :t myfoldr myfoldr :: (a -> b -> b) -> b -> [a] -> b ? myfoldr (+) 0 [1,2,3] 6 ?

Cse536 Functional Programming 24 7/14/2015 Functions returned as values Consider: k x = (\ y -> x) ? (k 3) 5 3 Another Example: plusn n = (\ x -> x + n) ? (plusn 4) 5 9 Is plusn different from plus ? why? – plus x y = x + y

Cse536 Functional Programming 25 7/14/2015 Additional Examples The webpage includes a link (in the lecture notes section, under today’s date, Sept. 29, 2004) to some additional examaples 1.Numerical Functions 1.Differentiation and square root. pp Fokker Notes 2.Primes. pp 29 Fokker Notes 3. Display Tool. pp 105 Reade Book 4.Numbers in Long Hand 5.Sorting. pp Reade Book 6.Making Change. Bird & Wadler