PROGRAMMING IN HASKELL プログラミング Haskell

Download PROGRAMMING IN HASKELL プログラミング Haskell

Post on 01-Jan-2016

31 views

Category:

Documents

2 download

Embed Size (px)

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

<ul><li><p>*PROGRAMMING IN HASKELLHaskellChapter 12 Lazy Evaluation (2011) http://www.cs.nott.ac.uk/~gmh/book.html </p></li><li><p>(evaluation, evaluate)(reduction, reduce): : a * x + a * y a (x + y) Haskell (unfold)(redex)()*</p></li><li><p>Haskell 3 :(lazy evaluation)Haskell *</p></li><li><p> square n = n * n square (3 + 4) : square (3 + 4){+ } = square 7{square } = 7 * 7{* } = 49</p><p>*</p></li><li><p>: square (3 + 4) {square } = (3 + 4) * (3 + 4) { + } = 7 * (3 + 4) {+ } = 7 * 7 {* } = 49</p><p>+ square </p><p>FACT: Haskell 2 *</p></li><li><p>(Reduction Strategies)() 2 : () ()2 ?</p><p>*</p></li><li><p>(Termination) loop = tail loop</p><p> fst (1, loop) 2 : fst (1, loop) = fst (1, tail loop) = fst (1, tail (tail loop)) = </p><p>*</p></li><li><p> fst (1, loop) = 1 1 </p><p>FACTS*</p></li><li><p>:</p><p>: 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()</p></li><li><p>:</p><p>: = + (Sharing)FACTS[]Haskell * </p></li><li><p>! ones :: [Int] ones = 1 : ones: ones = 1 : ones = 1 : 1 : ones = 1 : 1 : 1 : ones = ones 1 *</p></li><li><p> head ones : head ones = head (1 : ones) = head (1 : 1 : ones) = head (1 : 1 : 1 : ones) = head ones = head (1 : ones) = 1 1 *</p></li><li><p> ones head ones </p><p>: </p><p> ones = 1 : ones *</p></li><li><p> : ? take 5 ones [1,1,1,1,1] ? take 5 [1..] [1,2,3,4,5]: take 5 [1..] *: : : </p></li><li><p>: : 2,3,4, p p 2 </p><p>: 2 3 4 5 6 7 8 9 10 11 12 3 5 7 9 11 5 7 11 7 11 11 *</p></li><li><p> Haskell : primes :: [Int] primes = sieve [2..] sieve :: [Int] -&gt; [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,*</p></li><li><p> 10 : ? take 10 primes [2,3,5,7,11,13,17,19,23,29]15 : ? takeWhile (&lt; 15) primes [2,3,5,7,11,13]! </p><p>*</p></li><li><p>(12)(): (C Java ): (): + (Haskell )[]()()*</p></li><li><p> 12 [0,1,1,2,3,5,8,13,21,34, fibs :: [Integer] : 2 0 1 2 b </p><p> n fib :: Int Integer *</p></li><li><p>*</p></li><li><p>square (3 + 4)===49( * ) 7( * ) (3 + 4)</p></li></ul>