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

Post on 21-Jan-2016

40 views

Embed Size (px)

DESCRIPTION

PROGRAMMING IN HASKELL Haskell. Chapter 7 - Higher-Order Functions . ( 2011 ) http://www.cs.nott.ac.uk/~gmh/book.html . 2 . Introduction. - PowerPoint PPT PresentationTRANSCRIPT

<ul><li><p>*PROGRAMMING IN HASKELLHaskellChapter 7 - Higher-Order Functions (2011) http://www.cs.nott.ac.uk/~gmh/book.html </p></li><li><p>*Introductiontwice :: (a a) a atwice f x = f (f x)twice 1 2 </p></li><li><p>*?</p><p>(domain specific language, DSL)</p></li><li><p>*Map map map :: (a b) [a] [b]For example:> map (+1) [1,3,5,7]</p><p>[2,4,6,8]</p></li><li><p>*: map :map f xs = [f x | x xs]map f [] = []map f (x:xs) = f x : map f xs</p></li><li><p>*Filter filter filter :: (a Bool) [a] [a]For example:> filter even [1..10]</p><p>[2,4,6,8,10]</p></li><li><p>*:filter :filter p xs = [x | x xs, p x]filter p [] = []filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs</p></li><li><p>*(foldr)( v ):f [] = vf (x:xs) = x f xsf v ()( f ) </p></li><li><p>*For example:sum [] = 0sum (x:xs) = x + sum xsand [] = Trueand (x:xs) = x && and xsproduct [] = 1product (x:xs) = x * product xsv = 0 = +v = 1 = *v = True = &&</p></li><li><p>* foldr (fold right, ) () v For example:sum xs = foldr (+) 0 xs</p><p>product xs = foldr (*) 1 xs</p><p>or xs = foldr (||) False xs</p><p>and xs = foldr (&&) True xs()</p></li><li><p>*foldr :foldr :: (a b b) b [a] bfoldr f v [] = vfoldr f v (x:xs) = f x (foldr f v xs) foldr f v (x:xs) = x `f` (foldr f v xs)foldr cons (:) [] v 1, 2 ()</p></li><li><p> foldr () v [x0, x1, ..., xn] = foldr () v (x0 : x1 : ... : xn : []) = x0 (x1 (... ( xn v)))cons (:) [] v *</p></li><li><p>*sum [1,2,3]For example:Replace each (:)by (+) and [] by 0.</p></li><li><p>*product [1,2,3]For example:Replace each (:)by (*) and [] by 1.</p></li><li><p>*foldr foldr </p><p>length :length :: [a] Intlength [] = 0length (_:xs) = 1 + length xs</p></li><li><p>*length [1,2,3]Replace each (:) by _ n 1+n and [] by 0.:</p></li><li><p>_ n 1+n ? length [1, 2, 3] = foldr f v (1 : (2 : (3 : []))) = 1 `f` (2 `f` (3 `f` v)) where v = 0 f x y = 1 + yf 3 v f x y 3 x v y [3] 1 f 2 (f 3 v) f x y 2 x 1 y [2, 3] 2 f x y x y y 1 (v 0) f x y = 1 + y(x _ )*: 3 `f` v == f 3 v</p></li><li><p>*reverse :reverse [] = []reverse (x:xs) = reverse xs ++ [x]reverse [1,2,3]:Replace each (:) by x xs xs ++ [x]and [] by [].</p></li><li><p>*:reverse xs = foldr (x xs xs ++ [x]) [] xs (++) foldr :(++) xs ys = foldr (:) ys xsReplace each (:) by (:) and [] by ys.</p></li><li><p>x xs xs ++ [x] ? reverse [1,2,3] = foldr f v (1 : (2 : (3 : []))) = 1 `f` (2 `f` (3 `f` v)) where v = [] f x y = y ++ [x]f 3 v f x y 3 x v y [3] [3] f 2 (f 3 v) f x y 2 x [3] y [2,3] [3,2] f x y x y y [x] (v []) f x y = y ++ [x]</p><p>*: 3 `f` v == f 3 v</p></li><li><p>reverse snoc snoc x xs = xs ++ [x] snoc == x xs xs ++ [x] </p><p>* reverse [1,2,3]= r (1 : (2 : (3 : [])))= 1 `snoc` (2 `snoc` (3 `snoc` []))= (([] ++ [3]) ++ [2]) ++ [1]= [] ++ [3] ++ [2] ++ [1]= [3,2,1]</p></li><li><p>*:reverse xs = foldr (x xs xs ++ [x]) [] xs (++) foldr :(++) xs ys = foldr (:) ys xsReplace each (:) by (:) and [] by ys.</p></li><li><p>append foldr *</p></li><li><p>*foldr ?sum </p><p>foldr foldr (fusion banana split )</p><p> foldr </p></li><li><p>f [] = v f (x:xs) = x f xs f xs foldr () v xs foldr () v [x0, x1, ..., xn] = x0 (x1 (... (xn v)))</p><p>sum xs = foldr (+) 0 xsproduct xs = foldr (*) 1 xsand xs = foldr (&&) True xsfoldr ()*</p></li><li><p>* foldl foldr </p></li><li><p>*(foldl)( v ):f xs = f v xsf ac [] = acf ac (x:xs) = f (ac x) xsf ( )() f f: f ac: </p></li><li><p>*For example:sum xs = sum 0 xssum ac [] = acsum ac (x:xs) = sum (ac+x) xsrev xs = rev' [] xsrev' ac [] = acrev' ac (x:xs) = rev' (x:ac) xsproduct xs = prod 1 xsprod ac [] = acprod ac (x:xs) = prod (ac*x) xsv = 0 = +v = 1 = *v = [] = ys y -> y:ys </p></li><li><p>* foldl (fold left, ) () v For example:sum xs = foldl (+) 0 xs</p><p>product xs = foldl (*) 1 xs</p><p>reverse xs = foldl (ys y -> y:ys) [] xs</p></li><li><p>*foldl :foldl :: (a b a) a [b] afoldl f ac [] = acfoldl f ac (x:xs) = foldl f (f ac x) xsfoldl f ac (x:xs) = foldl f (ac `f` x) xsfoldl v ac 1 ()</p></li><li><p> fold () v [x0, x1, ..., xn] = foldl () v (x0 : x1 ... : xn : []) = (((v x0) x1) ...) xn</p><p> v *</p></li><li><p>*sum [1,2,3]For example: 0 (+) </p></li><li><p>*product [1,2,3]For example: 1 (*) </p></li><li><p>reverse foldl rev [1,2,3] = foldl (ys y -> y:ys) [] [1,2,3] = foldl () (() [] 1) [2,3] = foldl () (1:[]) [2,3] = foldl () (() [1] 2) [3] = foldl () (2:[1]) [3] = foldl () (() [2,1] 3) [] = foldl () (3:[2,1]) [] = [3,2,1]* ys </p></li><li><p>ys y -> y:ys ? reverse [1,2,3] = foldl f v (1 : (2 : (3 : []))) = ((v `f` 1) `f` 2) `f` 3 where v = [] f x y = y:xf v 1 f x y v x 1 y [1] [1] f (f v 1) 2 f x y [1] x 2 [1,2] [2,1] f x y x y x cons (v []) f x y = y:x</p><p>*: v `f` 1 == f v 1</p></li><li><p>f xs = f v xs f ac [] = ac f ac (x:xs) = f (ac x) xs f xs foldl () v xs ( )() f foldl () v [x0, x1, ..., xn] = (((v x0) x1) ...) xn</p><p>reverse xs = foldl (\ys y -> y:ys) [] xsfoldl ()*</p></li><li><p>*foldl </p></li><li><p>* (.) 2 (.) :: (b c) (a b) (a c)f . g = x f (g x)For example:odd :: Int Boolodd = not . evenodd = not . evenodd = x not (evenx)odd n = (not . even) nodd n = not (even n)odd n = not $ even n</p></li><li><p>* all all :: (a Bool) [a] Boolall p xs = and [p x | x xs]For example:> all even [2,4,6,8,10]</p><p>True</p></li><li><p>*all any 1 any :: (a Bool) [a] Boolany p xs = or [p x | x xs]For example:> any isSpace "abc def"</p><p>True</p></li><li><p>* takeWhile takeWhile :: (a Bool) [a] [a]takeWhile p [] = []takeWhile p (x:xs) | p x = x : takeWhile p xs | otherwise = []For example:> takeWhile isAlpha "abc def"</p><p>"abc"</p></li><li><p>*takeWhile dropWhile dropWhile :: (a Bool) [a] [a]dropWhile p [] = []dropWhile p (x:xs) | p x = dropWhile p xs | otherwise = x:xsFor example:> dropWhile isSpace " abc"</p><p>"abc"</p></li><li><p>(7): map: filter: foldr: foldr () v [x0, x1, ..., xn] = x0 (x1 (... (xn v)))sum xs = foldr (+) 0 xsproduct xs = foldr (*) 1 xsand xs = foldr (&&) True xs(.): f . g = x f (g x)all: takeWhile: *</p></li><li><p>(foldr foldl)v foldr v foldl </p><p>* foldr (+) 0 (1:(2:(3:[])))= 1 + (foldr (+) 0 (2:(3:[])))= 1 + (2 + (foldr (+) 0 (3:[])))= 1 + (2 + (3 + (foldr (+) 0 [])))= 1 + (2 + (3 + 0))= 6 foldl (+) 0 (1:(2:(3:[])))= foldl (+) (0 + 1) (2:(3:[]))= foldl (+) ((0 + 1) + 2) (3:[])= foldl (+) (((0 + 1) + 2) + 3) []= ((0 + 1) + 2) + 3= 6</p></li><li><p>*</p></li><li><p>:12345[]::::12345v</p></li><li><p>:12345[]::::54321v</p></li><li><p>foldr (:) ys:12345ys::::</p><p>**</p></li></ul>