24
Young Won Lim 12/4/17 Background – Functions (1C)

Background – Functions (1C) · 2017. 12. 4. · Background (1C) Functions 6 Young Won Lim 12/4/17 Functions : First-class Data Types functions are first-class data types Haskell

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

  • Young Won Lim12/4/17

    Background – Functions (1C)

  • Young Won Lim12/4/17

    Copyright (c) 2016 - 2017 Young W. Lim.

    Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".

    Please send corrections (or suggestions) to [email protected].

    This document was produced by using OpenOffice.

    mailto:[email protected]

  • Background (1C)Functions 3 Young Won Lim12/4/17

    Based on

    http://learnyouahaskell.com/making-our-own-types-and-typeclasses#the-functor-typeclass

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    Haskell in 5 stepshttps://wiki.haskell.org/Haskell_in_5_steps

    http://learnyouahaskell.com/making-our-own-types-and-typeclasses#the-functor-typeclasshttp://learnyouahaskell.com/functors-applicative-functors-and-monoids

  • Background (1C)Functions 4 Young Won Lim12/4/17

    Function Definition

    Function Definition I.

    square x = x * x - function type is inferred → not efficient Type Inference

    Function Definition II.

    square :: Double -> Double – function type declaration

    square x = x * x

    Function Type Declaration

    Type Declaration

    the declaration of an identifier's type

    the identifier name :: the type name ...

    type names in Haskell always begin with a capital letter,

    identifier names (including function identifiers) must always begin with a lower-case letter

    http://www.toves.org/books/hsfun/

    ● function type declaration ● function definition

  • Background (1C)Functions 5 Young Won Lim12/4/17

    Function Types and Type Classes

    Function Definition I.

    square x = x * x

    Function Definition II.

    square :: Double -> Double

    square x = x * x

    http://www.toves.org/books/hsfun/

    ● function type declaration

    function definition

    function definition

    ● function type 1● function type 2 ●

    ● function type n

    type class – a set of types

    =

    =

    Requirements

    Subclasses

  • Background (1C)Functions 6 Young Won Lim12/4/17

    Functions : First-class Data Types

    functions are first-class data types

    Haskell treats functions as regular data,

    just like integers, or floating-point values, or other types.

    ● a function can take other functions as parameters

    ● a function takes a parameter and produces another function (curried function)

    http://www.toves.org/books/hsfun/

    f x y

    (f x) y

    g y

    f :: a -> b -> c

    g :: b -> c

    f :: a -> (b -> c)

    f

    g

    x

    y z

    a (b->c)

    b c

    f :: a -> b -> c

    f x returns a function of type b -> c

  • Background (1C)Functions 7 Young Won Lim12/4/17

    Currying Examples

    fx

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    fy

    x

    fx

    y

  • Background (1C)Functions 8 Young Won Lim12/4/17

    Polymorphic Functions

    specific types vs. arbitrary types

    a polymorphic functions – an abstract type

    each type variable is generally a lower-case letter.

    Example) A translate function

    takes a function f and a distance d

    returns a new function g

    that is f "translated" d units to the right

    http://www.toves.org/books/hsfun/

  • Background (1C)Functions 9 Young Won Lim12/4/17

    Polymorphic Function Examples

    translate :: (Double -> Double) -> Double -> (Double -> Double)

    translate f d = g where g x = f (x – d)

    translate :: (Double -> a) -> Double -> (Double -> a)

    http://www.toves.org/books/hsfun/

    fx yDouble Double

    gx zDouble Double

    translated gDouble Double->Double

    fx – d zDouble Double

  • Background (1C)Functions 10 Young Won Lim12/4/17

    Currying

    Currying recursively transforms a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.

    f :: a -> b -> c

    https://wiki.haskell.org/Currying

    f x y

    (f x) y

    g y

    f :: a -> b -> c

    g :: b -> c

    f :: a -> (b -> c)

    f

    g

    x

    y z

    a (b->c)

    b c

    f :: a -> b -> c

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

  • Background (1C)Functions 11 Young Won Lim12/4/17

    Partially Applied Functions – f, (f x)

    f :: a -> b -> c -> d -> ef x y z w = … f

    xyzw

    f x

    xyzw

    g1 :: b -> c -> d -> eg1 y z w = …

    (f x) y z w

  • Background (1C)Functions 12 Young Won Lim12/4/17

    Partially Applied Functions – (f x y), (f x y z)

    g2 :: c -> d -> eg2 z w = … f x y

    xy

    zw

    f x y z

    xyz

    w

    g3 :: d -> eg3 w = …

    (f x y z) w

    (f x y) z w

  • Background (1C)Functions 13 Young Won Lim12/4/17

    Partially Applied Functions – g1, g2, g3

    f x

    xyzw

    f x y

    xy

    zw

    f x y z

    xyz

    w

    g1

    xyzw

    (f x) = g1

    g2

    xy

    zw

    g3

    xyz

    w

    (f x y) = g2

    (f x y z) = g3

  • Background (1C)Functions 14 Young Won Lim12/4/17

    Returning Functions

    ff

    x

    f x

    xyzw

    g1g1

    xy g1 y

    xy

    zw

    g1

    xyzw

    g2

    xy

    zw

    g2g2

    xy

    z g2 z

    xyz

    w

    g3

    xyz

    w

    f x returns g1 function

    g1 y returns g2 function

    g2 z returns g3 function

  • Background (1C)Functions 15 Young Won Lim12/4/17

    Currying Examples

    f

    g1

    x

    y

    f :: a -> b -> c -> d -> e

    g2z

    g3w z

    f :: a -> (b -> (c -> (d -> e)))

    d e

    c (d -> e)

    b (c -> (d -> e))

    (b -> (c -> (d -> e)))a ((((f x) y) z) w)

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

  • Background (1C)Functions 16 Young Won Lim12/4/17

    Currying Examples

    f

    g1

    x

    y

    f :: a -> b -> c -> d -> e

    g2z

    g3w z

    f :: a -> (b -> (c -> (d -> e)))

    d e

    c (d -> e)

    b (c -> (d -> e))

    (b -> (c -> (d -> e)))a ((((f x) y) w) z)

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    g1

  • Background (1C)Functions 17 Young Won Lim12/4/17

    Currying Examples

    f

    g1

    x

    y

    f :: a -> b -> c -> d -> e

    g2z

    g3w z

    f :: a -> (b -> (c -> (d -> e)))

    d e

    c (d -> e)

    b (c -> (d -> e))

    (b -> (c -> (d -> e)))a ((((f x) y) z) w)

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    g2

  • Background (1C)Functions 18 Young Won Lim12/4/17

    Currying Examples

    f

    g1

    x

    y

    f :: a -> b -> c -> d -> e

    g2z

    g3w z

    f :: a -> (b -> (c -> (d -> e)))

    d e

    c (d -> e)

    b (c -> (d -> e))

    (b -> (c -> (d -> e)))a ((((f x) y) z) w)

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

  • Background (1C)Functions 19 Young Won Lim12/4/17

    Currying Examples

    mult :: Int -> Int -> Int -> Int f :: a -> (b -> (c -> d))(((f x) y) z)

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    (((mult x) y) z)

    f

    x

    y

    z

    wInt

    IntInt

    Int

  • Background (1C)Functions 20 Young Won Lim12/4/17

    Currying Examples

    mult :: Int -> Int -> Int -> Int

    f :: Int -> (Int -> (Int -> Int))f x y z

    f x :: Int -> (Int -> Int)g1 :: Int -> (Int -> Int)g1 y z

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    f x y :: Int -> Intg2 :: Int -> Intg2 z

    mult x y z

    mult a1 y z = g1 y z

    mult a1 a2 z = g2 z

    mult a1 a2 a3

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

  • Background (1C)Functions 21 Young Won Lim12/4/17

    Currying Examples

    mult :: Int -> Int -> Int -> Int mult x y z

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    mult a1 y z

    mult a1 a2 z

    mult a1 a2 a3

    f

    g1

    x

    y

    g2z wInt Int

    Int (Int -> Int)

    (Int -> (Int -> Int))Int

  • Background (1C)Functions 22 Young Won Lim12/4/17

    Currying Examples

    fx

    mult :: Int -> Int -> Int -> Int

    http://learnyouahaskell.com/functors-applicative-functors-and-monoids

    fy x

    fz xy

    mult x y z

    mult a1 y z

    mult a1 a2 z

    mult a1 a2 a3

  • Background (1C)Functions 23 Young Won Lim12/4/17

    Curry & Uncurry

    f :: a -> b -> c the curried form of g :: (a, b) -> c

    f = curry gg = uncurry f

    f x y = g (x,y)

    the curried form is usually more convenient because it allows partial application.

    all functions are considered curried

    all functions take just one argument

    https://wiki.haskell.org/Currying

    f :: a -> b -> c g :: (a, b) -> c

    currying

    uncurrying

    f x y g (x,y)

    the curried form

  • Young Won Lim12/4/17

    References

    [1] ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf[2] https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf

    ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf

    Diapositiva 1Diapositiva 2Diapositiva 3Diapositiva 4Diapositiva 5Diapositiva 6Diapositiva 7Diapositiva 8Diapositiva 9Diapositiva 10Diapositiva 11Diapositiva 12Diapositiva 13Diapositiva 14Diapositiva 15Diapositiva 16Diapositiva 17Diapositiva 18Diapositiva 19Diapositiva 20Diapositiva 21Diapositiva 22Diapositiva 23Diapositiva 24