lecture #2, sept 29, 2004

25
Cse536 Functional Programming 1 07/04/22 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

Upload: hilda

Post on 18-Jan-2016

17 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

104/21/23

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

Page 2: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

204/21/23

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)

?

Page 3: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

304/21/23

Definition By Patterns• Example on Booleans

myand True False = Falsemyand True True = Truemyand False False = Falsemyand False True = False

• Order Matters– Variables in Patterns match anything

myand2 True True = Truemyand2 x y = False

– What happens if we reverse the order of the two equations above?

Pattern may contain constructorsconstructors are always capitalized

Page 4: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

404/21/23

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]}

Page 5: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

504/21/23

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)

Page 6: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

604/21/23

Recursive Functions & Comments

• File Contentsplus x y = if x == 0 then y else 1 + (plus (x-1) y)

len [] = 0

len (x:xs) = 1 + (len xs)

• Hugs session ? plus 3 5

8

? len [1,2,3,4]

4

Page 7: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

704/21/23

Comments

-- A comment to the end of the line

{- This is a comment to the next -}

{- Nesting of {- -} pairs allowed -}

Page 8: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

804/21/23

Local Definitions• Local Definitions: Where

len2 [] = 0len2 (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 fwhere a = f x y b = g z -- y is the name of fun -- being defined

• Location of <newline> makes a big difference

• RULE of thumb Definitions at the same scope should be indented equally far.

Page 9: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

904/21/23

Function Types & Prototyping

• Typingf a b c = a + b + c + 1

has type

f :: Int -> Int -> Int -> Int

Read as

f:: Int -> (Int -> (Int -> Int))

• Prototypingplus :: 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

Page 10: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1004/21/23

Overloading and Classes

? :type difference

difference:: Num a => a -> a -> a

? difference 3 4

-1

? difference 4.5 7.8

-3.3

?

The class Num is a predicate on types

Page 11: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1104/21/23

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]

Page 12: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1204/21/23

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]

Page 13: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1304/21/23

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]

– <<function>>

• Types– Int

– Int

– [ Int ]

– [ a ] -> Int

Page 14: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1404/21/23

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)

Page 15: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1504/21/23

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

Page 16: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1604/21/23

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

Page 17: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1704/21/23

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 ::

Page 18: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1804/21/23

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 6 + -

– level 5 : ++ \\

– level 4 == /= < <= > >= `elem`

– level 3 &&

– level 2 ||

– level 1 (not used in the prelude)

Page 19: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

1904/21/23

Associativity(FN pp 22-23)

• Right

– 2 : 3 : 4 : [] = 2 : (3 : (4 : []))

– Other Right Associative operators

(:) (^) (++) (&&) (||)

• Left– 7 - 6 - 2 = (7 - 6) - 2

– Other Left Associative operators

(!!) (-) (*) (+)

• Non-associative– (<) (>) ... (i.e. all the relational operators)

Page 20: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

2004/21/23

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

Page 21: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

2104/21/23

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]

Page 22: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

2204/21/23

When do you define a higher order function?

• Abstraction is the keymysum [] = 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

Page 23: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

2304/21/23

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

?

Page 24: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

2404/21/23

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

Page 25: Lecture #2,  Sept 29, 2004

Cse536 Functional Programming

2504/21/23

Additional ExamplesThe 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 30-32 Fokker Notes

2. Primes. pp 29 Fokker Notes

3. Display Tool. pp 105 Reade Book

4. Numbers in Long Hand

5. Sorting. pp 106-109 Reade Book

6. Making Change. Bird & Wadler