1-nov-15 haskell ii functions and patterns. data types int + - * / ^ even odd float + - * / ^ sin...

32
Jun 27, 2 022 Haskell II Functions and patterns

Upload: martin-carson

Post on 04-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Apr 20, 2023

Haskell II

Functions and patterns

Page 2: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Data Types

Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool && || not Lists : ++ head tail last init take Tuples fst snd Polymorphic: < <= == /= => > show

Page 3: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

User-Defined Data Types

User-defined data types data Color = Red | Blue toString Red = "red"

toString Blue = "blue" data Tree a =

Leaf a | Branch (Tree a) (Tree a) Can be tricky to use

Page 4: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Assorted Syntax

Comments are -- to end of lineor {- to -} (these may be nested)

Types are capitalized, variables are not Indentation may be used in place of braces Infix operators: + - `mod` `not` Prefix operators: (+) (-) mod not Types: take :: Int -> [a] -> [a]

Page 5: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Layout

The first nonblank character following where, let, or of determines the starting column let x = a + b

y = a * bin y / x

If you start too far to the left, that may end an enclosing clause

You can use { } instead, but this is not usually done

Page 6: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Infinite Lists

[1..5] == [1, 2, 3, 4, 5] [1..] == all positive integers [5, 10..32] == [5, 10, 15, 20, 25, 30] [5, 10..] == positive multiples of 5 [x*x | x <- [1..]] == squares of positive ints [x*x | x <- [1..], even x] == squares of positive

even ints [(x, y) | x <- [1..10], y <- [1..10], x < y]

Page 7: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool
Page 8: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Functions are also data

Functions are “first-class objects” Functions can be assigned Functions can be passed as parameters Functions can be stored in data structures There are operations on functions

But functions can’t be tested for equality Theoretically very hard!

Page 9: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Anonymous Functions

Form is \ parameters -> body Example: \x y -> (x + y) / 2

the \ is pronounced “lambda” the x and y are the formal parameters

inc x = x + 1 this is shorthand for inc = \x -> x + 1

add x y = x + y this is shorthand for add = \x y -> x + y

Page 10: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Currying

Technique named after Haskell Curry Functions only need one argument Currying absorbs an argument into a function f a b = (f a) b, where (f a) is a curried function (avg 6) 8

7.0

Page 11: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Slicing

Functions may be “partially applied” inc x = x + 1

can be defined instead as inc = (+ 1) add x y = x + y

can be defined instead as add = (+) negative = (< 0)

Page 12: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

map

map :: (a -> b) -> [a] -> [b] applies the function to all elements of the list Prelude> map odd [1..5]

[True,False,True,False,True] Prelude> map (* 2) [1..5]

[2,4,6,8,10]

Page 13: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

filter

filter :: (a -> Bool) -> [a] -> [a] Returns the elements that satisfy the test Prelude> filter even [1..10]

[2,4,6,8,10] Prelude> filter (\x -> x>3 && x<10) [1..20]

[4,5,6,7,8,9]

Page 14: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

iterate

iterate :: (a -> a) -> a -> [a] f x returns the list [x, f x, f f x, f f f x, …] Prelude> take 8 (iterate (2 *) 1)

[1,2,4,8,16,32,64,128] Prelude> iterate tail [1..3]

[[1,2,3],[2,3],[3],[], *** Exception: Prelude.tail: empty list

Page 15: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

foldl

foldl :: (a -> b -> a) -> a -> [b] -> a foldl f i x starts with i, repeatedly applies f to i and the

next element in the list x Prelude> foldl (-) 100 [1..3]

94 94 = 100 - 1 - 2 - 3

Page 16: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

foldl1

foldl1 :: (a -> a -> a) -> [a] -> a Same as: foldl f (head x) (tail x) Prelude> foldl1 (-) [100, 1, 2, 3]

94 Prelude> foldl1 (+) [1..100]

5050

Page 17: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

flip

flip :: (a -> b -> c) -> b ->a -> c Reverses first two arguments of a function Prelude> elem 'o' "aeiou"

True Prelude> flip elem "aeiou" 'o'

True Prelude> (flip elem) "aeiou" 'o'

True

Page 18: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Function composition with (.)

(.) :: (a -> b) -> (c -> a) -> (c -> b) (f . g) x is the same as f (g x) double x = x + x

quadruple = double . doubledoubleFirst = (* 2) . head

Main> quadruple 312Main> doubleFirst [3..10]6

Page 19: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

span

span :: (a -> Bool) -> [a] -> ([a], [a]) Break the lists into two lists

those at the front that satisfy the condition the rest

Main> span (<= 5) [1..10] ([1,2,3,4,5],[6,7,8,9,10])

Main> span (< 'm') "abracadabra" ("ab","racadabra")

Page 20: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

break

break :: (a -> Bool) -> [a] -> ([a], [a]) Break the lists into two lists

those at the front that fail the condition the rest

Main> break (== ' ') "Haskell is neat!" ("Haskell"," is neat!")

Page 21: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Function Definition I

Functions are defined with = fact n =

if n == 0 then 1 else n * fact (n - 1)

Page 22: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Function Definition II

Functions are usually defined by cases fact n

| n == 0 = 1 | otherwise = n * fact (n - 1)

fact n = case n of 0 -> 1 n -> n * fact (n - 1)

These are “the same”

Page 23: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Function Definition III

You can separate the cases with “patterns” fact :: Int -> Int -- not essential

fact 0 = 1fact n = n * fact (n - 1)

How does this work?

Page 24: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Pattern Matching

Functions cannot in general be overloaded But they can be broken into cases Each case must have the same signature fact :: Int -> Int -- explicit signature

fact 0 = 1fact n = n * fact (n - 1)

fact 5 won’t match the first, but will match the second

Page 25: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Pattern Types I

A variable will match anything A wildcard, _, will match anything, but you can’t use

the matched value A constant will match only that value Tuples will match tuples, if same length and

constituents match Lists will match lists, if same length and constituents

match However, the pattern may specify a list of arbitrary length

Page 26: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Pattern Types II

(h:t) will match a nonempty list whose head is h and whose tail is t

second (h:t) = head t Main> second [1..5]

2

Page 27: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Pattern Types III

“As-patterns” have the form w@pattern When the pattern matches, the w matches the whole of

the thing matched firstThree all@(h:t) = take 3 all Main> firstThree [1..10]

[1,2,3]

Page 28: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Pattern Types IV

(n+k) matches any value equal to or greater than k; n is k less than the value matched

silly (n+5) = n Main> silly 20

15 This is the only arithmetical pattern; it does not

generalize to any other pattern

Page 29: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Advantages of Haskell

Extremely concise Easy to understand

no, really! No core dumps Polymorphism improves chances of re-use Powerful abstractions Built-in memory management

Page 30: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

Disadvantages of Haskell

Unfamiliar Slow

because compromises are less in favor of the machine

Page 31: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

quicksort

quicksort [] = []

quicksort (x:xs) = quicksort [y | y <- xs, y < x] ++ [x] ++ quicksort [y | y <- xs, y >= x]

Page 32: 1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool

The End