Post on 01-Jan-2016

32 views

Category:

## Documents

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

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

• 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

• (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()

• :

• ! 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 = 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)