haskell programming and stever/haskell_notes.pdffunctional programming and haskell stevedreeves...

Download HASKELL PROGRAMMING AND  stever/Haskell_notes.pdfFunctional Programming and Haskell StevedReeves 2006 University ofeWaikato 1 FUNCTIONAL PROGRAMMING AND HASKELL ... of elements in the list xs

Post on 13-May-2018

216 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 1

    FUNCTIONAL

    PROGRAMMING AND

    HASKELL

    SOME ELEMENTARY NOTES

    Steve Reeves Department of Computer Science

    University of Waikato

    Functional Programming and HaskellSteve Reeves2006University of Waikato 2

    Many of these notes are based on

    Haskell: The Craft of Functional Programming, Simon Thompson, Addison-Wesley, 1996 This introduction is based on chapter one of:

    Functional Programming with Miranda, Ian Holyer, UCL Press, 1993 which is in the Library.

    Other good books, all in the Library, on functional programming are:

    Miranda: The Craft of Functional Programming, Simon Thompson, Addison-Wesley, 1995 Introduction to Functional Programming, Richard Bird and Philip Wadler, Prentice-Hall, 1988

    Elements of Functional Programming, Chris Reade, Addison-Wesley, 1989

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 3

    Also, search the Library catalogues for functional programming - there are about 12 more books on the subject in our Library

    Functional Programming and HaskellSteve Reeves2006University of Waikato 4

    Taxonomy

    specification

    declarative logical functional languages structured procedural high-level low-level

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 5

    Procedural Languages

    developed first in the 1950s

    most languages today are procedural

    two common ideas - instructions executed sequentially - values stored in locations brought together by defining sequences

    of instructions to change the contents of storage - procedures, routines

    these languages closely match the

    architecture of most of today!s computers

    - advantage is efficiency - disadvantage is unnecessary

    detail

    Functional Programming and HaskellSteve Reeves2006University of Waikato 6

    Evolution machine code low-level

    assembly high-level ForTran, BASIC structured C, Pascal, Modula object-oriented Smalltalk

    C++

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 7

    Declarative Languages

    origins in the !-calculus of 1930s

    became practical in the 1970s at a higher-level than any of the

    procedural languages breaks away from the procedural model allows expression of algorithms in a very

    clear and direct way few unnecessary details values can be defined and manipulated

    directly no need to worry about how or where

    they are stored

    algorithms are given by declaring

    relationships between values - no concern about the order used

    Functional Programming and HaskellSteve Reeves2006University of Waikato 8

    Various sorts of expressions declare

    relations between values

    Logic programming languages use

    relations Specification languages use any

    mathematical structures you like - they are more general and

    expressive than either logic of functional languages

    - so expressive that you can write

    down relations that are not computable!!

    Functional languages use functions

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 9

    Programming with Functions

    Functional programming languages started in the 1980s (LISP started in the 1950s but is hybrid, i.e. combines functional and procedural features) languages like Miranda, Haskell and Gofer

    are all very similar

    Functional Programming and HaskellSteve Reeves2006University of Waikato 10

    Functions for Programming In a functional language, functions are the

    basic building blocks from which programs are constructed

    A program is a function from its inputs to

    its output:

    inputsprogram

    output

    This gives rise to two important

    differences between functions and procedures:

    functions have no side effects they can have no effect on the

    world outside except to deliver a result value

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 11

    functions have no state they give the same result every

    time they are evaluated with the same arguments

    this means that functions form self-contained

    units all their connections with the

    outside world are explicit they are therefore safe and

    convenient building blocks

    Functional Programming and HaskellSteve Reeves2006University of Waikato 12

    two most common ways of combining functions

    pass the results of one to the

    arguments of another fourth_power n = square (square n)

    pass a function as an argument to

    another function

    square_list ns = map square ns where map f [x1,x2,x3,...,xn] = [f x1, f x2,...,f xn] so square_list [1,2,3,4] = [1,4,9,16]

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 13

    map is an example of a very useful and common value in functional programming, the higher-order function

    Functional Programming and HaskellSteve Reeves2006University of Waikato 14

    Gofer and Haskell They are lazy, polymorphic, purely

    functional programming languages

    Developed following the Haskell Report We shall be using HUGS-Haskell User!s

    Gofer System-Available, free, for PCs (IBM-style and Macs), Ataris, Amigas, larger Unix machines and others

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 15

    Using HUGS In the labs we will be using HUGS running

    under Linux

    To run the interpreter just type hugs Typing :? will show you commands that can be

    used :l will load a file called , which you

    can fill with definitions, or a script, using any editor like emacs, vi or textedit (under X)

    Functional Programming and HaskellSteve Reeves2006University of Waikato 16

    any definitions you load from a file will augment the standard definitions which are always available, so extending the set of expressions that can be evaluated

    any expressions you type at the prompt ?

    will be evaluated and their value printed as output:

    ? 2 + 3 5 ? sum [1..10] 55 ? product [1..6] 720

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 17

    Data Simple data or unstructured data - numbers

    42 :: Int i.e. 42 has type Int 3.14159 :: Fractional - operations on numbers (+) :: Int -> Int -> Int (+) 2 3 :: Int (+) 2 3 = 5

    more usually written 2 + 3, these forms are equivalent

    (*),(-),(/),(^) and many more - characters a :: Char

    Functional Programming and HaskellSteve Reeves2006University of Waikato 18

    0 :: Char \n :: Char - operations ord :: Char -> Int chr :: Int -> Char ord a = 97 chr 65 = A - Booleans True :: Bool False :: Bool - operations (&&) :: Bool -> Bool -> Bool not :: Bool -> Bool True && True = True

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 19

    You can get HUGS to tell you the type of

    expressions by typing :t ord for example, which will give the response ord :: Char -> Int

    Functional Programming and HaskellSteve Reeves2006University of Waikato 20

    Structured Data - Lists given a type t, values of type

    [t] are lists whose elements have type t

    [1,2,3,4] :: [Int] [a,b,c,d] :: [Char] [True, True, False] :: [Bool] [[1,2],[1,5],[1],[2,3]] :: [[Int]]

    [] :: [Char] [] :: [Int] lists can also be written using

    the constructor : [1,2,3] = 1 : 2 : 3 : []

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 21

    = 1 : [2,3] = 1 : 2 :[3] :: [Int]

    Functional Programming and HaskellSteve Reeves2006University of Waikato 22

    - operations length xs returns the number

    of elements in the list xs xs ++ ys returns the list of

    elements in xs followed by the elements in ys

    map f xs returns the list

    obtained by applying the function f to each of the elements of the list xs

    length [1,2,3,4] = 4 length [a,b,c] = 3 [1,2] ++ [4,5] = [1,2,4,5] map ord [a,b] = [97, 98]

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 23

    - special lists [n..m] is the list of integers from n to m [3..5] = [3,4,5] The list with values of the form [n,n+k..m] is the list [n,n+k,n+2k,n+3k..n+dk,m] where

    n+dk " m < n + (d+1)k [1,3..11] = [1,3,5,7,9,11] [n..] is the list [n,n+1,n+2,n+3...]

    Functional Programming and HaskellSteve Reeves2006University of Waikato 24

    i.e. no matter how many times you take away the first element, there is always more list - a sort of infinite list on numbers starting from n

  • Functional Programming and HaskellSteve Reeves2006University of Waikato 25

    - tuples if t1, t2,..., tn (n#2) are types

    then (t1, t2,...,tn) is the type of n-tuples of the form (x1,x2,...,xn) where x1 has type t1, x2 has type t2 an