PROGRAMMING IN HASKELL Haskell

PROGRAMMING IN HASKELL  Haskell slide 0
Download PROGRAMMING IN HASKELL  Haskell

Post on 01-Jan-2016

30 views

Category:

Documents

2 download

DESCRIPTION

PROGRAMMING IN HASKELL Haskell. Chapter 12 Lazy Evaluation . ( 2011 ) http://www.cs.nott.ac.uk/~gmh/book.html . . (evaluation, evaluate) (reduction, reduce) : - PowerPoint PPT Presentation

TRANSCRIPT

  • *PROGRAMMING IN HASKELLHaskellChapter 12 Lazy Evaluation (2011) http://www.cs.nott.ac.uk/~gmh/book.html

  • (evaluation, evaluate)(reduction, reduce): : a * x + a * y a (x + y) Haskell (unfold)(redex)()*

  • Haskell 3 :(lazy evaluation)Haskell *

  • square n = n * n square (3 + 4) : square (3 + 4){+ } = square 7{square } = 7 * 7{* } = 49

    *

  • : square (3 + 4) {square } = (3 + 4) * (3 + 4) { + } = 7 * (3 + 4) {+ } = 7 * 7 {* } = 49

    + square

    FACT: Haskell 2 *

  • (Reduction Strategies)() 2 : () ()2 ?

    *

  • (Termination) loop = tail loop

    fst (1, loop) 2 : fst (1, loop) = fst (1, tail loop) = fst (1, tail (tail loop)) =

    *

  • fst (1, loop) = 1 1

    FACTS*

  • :

    : square 3 + 4 2 FACT: * (3):(4): square (3 + 4) square (3 + 4) = square 7= (3 + 4) * (3 + 4) = 7 * 7= 7 * (3 + 4) = 49= 7 * 7= 49()

  • :

    : = + (Sharing)FACTS[]Haskell *

  • ! ones :: [Int] ones = 1 : ones: ones = 1 : ones = 1 : 1 : ones = 1 : 1 : 1 : ones = ones 1 *

  • head ones : head ones = head (1 : ones) = head (1 : 1 : ones) = head (1 : 1 : 1 : ones) = head ones = head (1 : ones) = 1 1 *

  • ones head ones

    :

    ones = 1 : ones *

  • : ? take 5 ones [1,1,1,1,1] ? take 5 [1..] [1,2,3,4,5]: take 5 [1..] *: : :

  • : : 2,3,4, p p 2

    : 2 3 4 5 6 7 8 9 10 11 12 3 5 7 9 11 5 7 11 7 11 11 *

  • Haskell : primes :: [Int] primes = sieve [2..] sieve :: [Int] -> [Int] sieve (p:xs) = p:sieve [x|x xs, x `mod` p 0]: ? primes [2,3,5,7,11,13,17,19,23,29,31, 37,41,43,47,53,59,61,67,*

  • 10 : ? take 10 primes [2,3,5,7,11,13,17,19,23,29]15 : ? takeWhile (< 15) primes [2,3,5,7,11,13]!

    *

  • (12)(): (C Java ): (): + (Haskell )[]()()*

  • 12 [0,1,1,2,3,5,8,13,21,34, fibs :: [Integer] : 2 0 1 2 b

    n fib :: Int Integer *

  • *

  • square (3 + 4)===49( * ) 7( * ) (3 + 4)