comp313a programming languages n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
COMP313A Programming Languages PowerPoint Presentation
Download Presentation
COMP313A Programming Languages

Loading in 2 Seconds...

play fullscreen
1 / 24

COMP313A Programming Languages - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

COMP313A Programming Languages. Functional Programming (5). Lecture Outline. Higher order functions Functions as arguments Some recapping and exercises Some more functions as data and results. Expressions Defining Functions. addnum :: Int -> (Int -> Int) addnum n = addN

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'COMP313A Programming Languages' - quanda


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
comp313a programming languages

COMP313A Programming Languages

Functional Programming (5)

lecture outline
Lecture Outline
  • Higher order functions
  • Functions as arguments
  • Some recapping and exercises
  • Some more functions as data and results
expressions defining functions
Expressions Defining Functions

addnum :: Int -> (Int -> Int)

addnum n = addN

where -- local definition

addN m = n + m

When addnum 10 say is called returns a function addN which adds 10 to m

slide4

test :: Int -> Int -> Int

test x y

| x >= y = somefun f y

| otherwise = 4

where

f = addnum 4

lambda in haskell
Lambda in Haskell

\m -> n + m

addNum n = (\m -> n + m)

Write a function “test n” that returns a function which tests

if some argument is <= to n. Use a lambda.

partial application of functions
Partial Application of Functions

add :: Int -> Int -> Int

add x y = x+y

4

5

4

partial application of functions1
Partial Application of Functions…

add4 :: [Int] -> [Int]

add4 xs = map (add 4) xs

add4 :: ([Int] -> [Int])

add4 = map (add 4)

How would we use partial applications of functions to get the

same result as the “addNum n” example?

types of partial applications
Types of partial applications

The type of function is

t1 -> t2 -> … tn -> t

and it is applied to arguments

e1 :: t1, e2 :: t2 … , ek :: tk

if k <= n (partial application) then cancel the ones that match t1 – tk

leaving

tk+1 -> tk+2 -> … -> tn

types of partial function application
Types of Partial Function Application

add :: Int -> Int -> Int

add 2 :: Int ->Int

add 2 3 :: Int

operator sections
Operator Sections

(*2)

(2*)

(>2)

(6:)

(\2)

filter (>0) . map (+1)

Find operator sections sec1 and sec2 so that

map sec1. filter sec2

Has the same effect as filter (>0) . map (+1)

partial application of functions and operator sections
Partial Application of Functionsand Operator Sections

elem :: Char -> [Char] -> Bool

elem ch whitespace

where whitespace is the string “ \t\n”

partial application of functions and operator sections1
Partial Application of Functionsand Operator Sections

i.e. whitespace = “ \t\n”

The problem with partial application of function is that the argument of interest may not always be the first argument

So

member xs x = elem x xs

and

member whitespace

Alternatively we can use a lambda function

\ch -> elem ch whitespace

partial application of functions and operator sections2
Partial Application of Functionsand Operator Sections

To filter all non-whitespace characters from a string

filter (not . member whitespace)

filter (\ch -> not (elem ch whitespace))

write a recursive function to extract a word from a string
Write a recursive function to extract a word from a string

whitespace = “ \n\t”

getword :: String -> String

getword [ ] = [ ]

getword (x : xs)

| --how do we know we have a word

| -- otherwise build the word

- - recursively

getword “the quick brown”

write a recursive function to extract a word from a string1
Write a recursive function to extract a word from a string

Can write something more general - pass the “test” as an argument

getUntil :: (a -> Bool) -> [a] -> [a]

getUntil p [ ] = [ ]

getUntil p (x:xs)

| p x = [ ]

| otherwise = x : getUntil p xs

write a recursive function to extract a word from a string2
Write a recursive function to extract a word from a string

Okay but now how do we get a word

getWord xs

= getUntil p xs

where - - local definition

p x = member whitespace x

write a recursive function to extract a word from a string3
Write a recursive function to extract a word from a string

But we don’t really need the local definition

We can use our partial function instead

getWord xs

= getUntil p xs

where - - local definition

p x = member whitespace x

getWord xs

= getUntil (member whitespace) xs

write a recursive function to extract a word from a string4
Write a recursive function to extract a word from a string

Finally

The last word

getWord = getUntil (member whitespace)

---get characters until a whitespace is found

currying and uncurrying
Currying and Uncurrying
  • functions of two or more arguments take arguments in sequence, one at a time.
    • this is the curried form.
    • named after Haskell Curry
  • Uncurried version puts the arguments into a pair

addUC :: (Int, Int) -> Int

addUC (x,y) = (x + y)

curried versus uncurried
curried versus uncurried
  • Curried has neater notation
  • Curried permits partial application
  • Can easily convert from one to the other

curry f (x y) = f x y

uncurry f x y = f (x y)

type checking in haskell monomorphic type checking
Type Checking in HaskellMonomorphic Type Checking
  • Expressions

literal, variable, constant, function applied to some arguments

  • type checking function application
    • what do we need to consider
slide22

f must have a function type t

e must have type s

f e

the result has type t

slide23

ord ‘c’ +Int 3Int

ord ‘c’ +Int False

type checking function definitions
Type Checking Function Definitions

fib :: Int ->Int

fib n

| n == 0 = 0

| n == 1 = 1

| n >1 = fib (n-2) + fib (n-1)

  • Each of the guards must be of type ?
  • The value returned in each clause must be of type ?
  • The pattern n must be consistent with type argument ?