• Like
  • Save
Haskellで学ぶ関数型言語
Upcoming SlideShare
Loading in...5
×
 

Haskellで学ぶ関数型言語

on

  • 1,613 views

社内勉強会#2用の資料

社内勉強会#2用の資料

Statistics

Views

Total Views
1,613
Views on SlideShare
1,611
Embed Views
2

Actions

Likes
6
Downloads
0
Comments
0

2 Embeds 2

http://s.deeeki.com 1
http://www.google.co.jp 1

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Haskellで学ぶ関数型言語 Haskellで学ぶ関数型言語 Presentation Transcript

  • Haskell
  • ?
  • 1 public class Counter {23 private int count = 0;45 /**6 * . 7 * @param value 8 * @return 9 */10 public int up(int value) {11 count += value;12 return count;13 }14 }
  • 1 public class Counter {23 private int count = 0;45 /**6 * . 7 * @param value 8 * @return 9 */10 public int up(int value) {11 count += value;12 return count;13 }14 }
  • 1 import java.util.Scanner; 2 3 public class IOSample { 4 5 public static void main(String[] args) { 6 7 Scanner scan = new Scanner(System.in); 8 9 System.out.println(" ");10 String str = scan.next(); //11 System.out.println(" "+ str);12 }13 } ( )
  • 1 import java.util.Scanner; 2 3 public class IOSample { 4 5 public static void main(String[] args) { 6 7 Scanner scan = new Scanner(System.in); 8 9 System.out.println(" ");10 String str = scan.next(); //11 System.out.println(" "+ str);12 }13 } ( )
  • HaskellConcurrent Clean OCaml Erlang Scala Lisp(Scheme) F#
  • Whats faster than C++, more concise than Perl,more regular than Python, more flexible than Ruby,more typeful than C#, more robust than Java,and has absolutely nothing in common with PHP?Its Haskell! -- Autrijus Tang( )C++ PerlPython RubyC# JavaPHPHaskell
  • 1 cond :: Bool 2 cond = True 3 4 price :: Int 5 price = 2800 6 7 pi :: Float 8 pi = 3.141592653589793 910 greeting :: String11 greeting = "Hello, Haskell!"1213 letter :: Char14 letter = C
  • ‘A’ ‘B’ ‘C’ ‘D’1 bools = [True, False, True] :: [Bool]23 -- "ABCD"4 letters = [A, B, C, D] :: [Char]56 --7 empty = [] :: [Int]89 -- 110 notEmpty = [[]] :: [[Float]]
  • 1 --2 (False, A) :: (Bool, Char)34 -- ( )5 ("Name", True, 123) :: (String, Bool, Int)67 -- 8 (a, (False, b)) :: (Char, (Bool, Char)) 910 --11 ([a, b, ‘c’], [False, True]) :: ([Char], [Bool])1213 --14 [(a, False), (b, True)] :: [(Char, Bool)]
  • 1 double :: Int -> Int2 double x = x + x34 add :: (Int, Int) -> Int5 add (x, y) = x + y67 mult :: Int -> Int -> Int -> Int8 mult x y z = x * y * z> double 36> add (1, 7)8> mult 7 8 9504
  • 1 add :: (Int, Int) -> Int 2 add (x, y) = x + y 3 4 -- Int -> (Int -> Int) Haskell 5 add :: Int -> Int -> Int 6 add x y = x + y 1 7 8 mult10 :: Int -> Int -> Int 9 mult10 = mult 101011 mult30 :: Int -> Int12 mult30 = mult 10 3> mult10 3 8240> mult30 5150
  • 1 twice :: (Int -> Int) -> Int -> Int2 twice f x = f (f x)3 Int4 isDigit :: Char -> Bool Int5 isDigit c = c >= 0 && c <= 967 -- map :: (a -> b) -> [a] -> [b]> twice (*2) 312> map (+1) [1, 2, 3, 4][2, 3, 4, 5]> map isDigit [a, 0, b, 9][False, True, False, True]
  • > length [1, 3, 5, 7]4> length [True, False]2> length [length, length, length]3length :: [a] -> Intmap :: (a -> b) -> [a] -> [b]
  • > 1 + 23> (*) 1.1 2.3 -- 1.1 * 2.32.53> 10 `div` 2 -- div 10 25 1 (+) :: Num a => a -> a -> a 2 (*) :: Num a => a -> a -> a 3 negate :: Num a => a -> a 4 abs :: Num a => a -> a
  • 1 -- ( Ord, Show, Read )2 class Eq a where3 (==) :: a -> a -> Bool4 (/=) :: a -> a -> Bool56 -- MyType Eq7 instance Eq MyType where8 (MyType a) == (MyType a) = (a == a)
  • 1 -- [λxy. x+y]2 add x y = x + y3 add = x -> (y -> x + y)4 add = x y -> x + y56 --7 -- const x y = x 8 const :: a -> (b -> a) 9 const x = _ -> x1011 --> map (x -> x * 2 + 1) [1, 3, 5, 7][3, 7, 11, 15]
  • > [x^2 | x <- [1..5]][1,4,9,16,25]> [(x, y) | x <- [1,2,3], y <- [4,5]][(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)]1 --2 factors :: Int -> [Int]3 factors n = [x | x <- [1..n], n `mod` x == 0]4 --5 primes :: Int -> [Int]6 primes n = [x | x <- [2..n], factors x == [1, x]]> factors 100[1,2,4,5,10,20,25,50,100]> primes 50[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
  • 1 --2 factorial :: Int -> Int3 factorial 0 = 14 factorial n = n * factorial (n - 1)56 --7 product :: Num a => [a] -> a8 product [] = 19 product (n:ns) = n * product ns 1 2 3 4 1:[2,3,4] 4:[]
  • 1 qsort :: Ord a => [a] -> [a]2 qsort [] = []3 qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger4 where5 smaller = [a|a <- xs, a <= x]6 larger = [a|a <- xs, a > x]> qsort [100, 23, 37, 3, 55, 68, 49][3,23,37,49,55,68,100]
  • 1 even :: Int -> Bool even 42 even 0 = True = {even }3 even n = odd (n - 1) odd 34 = {odd }5 odd :: Int -> Bool even 26 odd 0 = False = {even }7 odd n = even (n - 1) odd 1 = {odd }> even 4 even 0True = {even }> odd 4 TrueFalse
  • 1 -- !2 summary :: [Integer] -> Integer3 summary [] = 04 summary (n:ns) = n + summary ns56 -- ( ) 7 summary :: [Integer] -> Integer 8 summary xs = f xs 0 9 where10 f [] sum = sum11 f (y:ys) sum = f ys (y + sum)
  • mult (x, y) = x * y ? > mult (1+2, 2+3) ? mult (1+2, 2+3) mult (1+2, 2+3)= { + } = {mult } mult (3, 2+3) (1+2) * (2+3)= {+ } = { + } mult (3, 5) 3 * (2+3)= {mult } = {+ } 3 * 5 3 * 5= {* } = {* } 15 15
  • inf :: Int inf = 1 + inf fst (0, inf) fst (0, inf)= {inf } = {fst } fst (0, 1+inf) 0= {inf } fst (0, 1+(1+inf))= {inf } fst (0, 1+(1+(1+inf)))= {inf } !!
  • 1 ones :: [Int] 2 ones = 1 : ones 3 4 -- ( ) 5 sieve :: [Int] -> [Int] 6 sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p /= 0] 7 8 primes :: [Int] 9 primes = sieve [2..] head ones = {head ones }> head ones head (1:ones)1 = {head }> take 10 primes 1[2,3,5,7,11,13,17,19,23,29]
  • Mac OS X 10.7.1tarai :: Int -> Int -> Int -> Int 1.8 GHz Intel Core i7tarai x y z 4 GB 1333 MHz DDR3 | x <= y = y | otherwise = tarai (tarai (x-1) y z) > tarai 16 10 0 (tarai (y-1) z x) 16 (tarai (z-1) x y) Haskellprivate int tarai(int x, int y, int z) { :107039ms if (x <= y) { return y; :45,722,185,221 } else { return tarai(tarai(x-1, y, z), tarai(y-1, z, x), tarai(z-1, x, y)); }} Java
  • Monad
  • Haskell (※ )
  • 6 -- set1 set2 7 translate :: String -> String -> Char -> Char 8 translate [] _ c = c 9 translate (x:xs) [] c = if x == c then else translate xs [] c10 translate (x:xs) [y] c = if x == c then y else translate xs [y] c11 translate (x:xs) (y:ys) c = if x == c then y else translate xs ys c1213 --14 translateString :: String -> String -> String -> String15 translateString set1 set2 str = map (translate set1 set2) str1617 usage :: IOError -> IO ()18 usage e = do putStrLn "Usage: ex14 set1 set2"19 putStrLn "Translates characters in set1 on stdin to the corresponding"20 putStrLn "characters from set2 and writes the translation to stdout."2122 --23 main :: IO ()24 main = (do [set1,set2] <- getArgs25 contents <- hGetContents stdin26 putStr $ translateString set1 set2 contents) `catchError` usage
  • 6 -- set1 set2 7 translate :: String -> String -> Char -> Char 8 translate [] _ c = c 9 translate (x:xs) [] c = if x == c then else translate xs [] c10 translate (x:xs) [y] c = if x == c then y else translate xs [y] c11 translate (x:xs) (y:ys) c = if x == c then y else translate xs ys c1213 --14 translateString :: String -> String -> String -> String15 translateString set1 set2 str = map (translate set1 set2) str1617 usage :: IOError -> IO ()18 usage e = do putStrLn "Usage: ex14 set1 set2"19 putStrLn "Translates characters in set1 on stdin to the corresponding"20 putStrLn "characters from set2 and writes the translation to stdout."2122 --23 main :: IO ()24 main = (do [set1,set2] <- getArgs25 contents <- hGetContents stdin26 putStr $ translateString set1 set2 contents) `catchError` usage
  • Monad
  • 1 class Monad m where 2 (>>=) :: m a -> (a -> m b) -> m b 3 return :: a -> m a 4 5 -- data Bool = False | True 6 data Maybe a = Nothing | Just a 7 8 instance Monad Maybe where 9 return = Just ( )10 fail = Nothing11 Nothing >>= f = Nothing Just Nothing12 (Just x) >>= f = f x 1 Maybe Int
  • lookup :: Eq a => a -> [(a, b)] -> Maybe bplayers = [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")]> lookup 11 playersJust "Darvish"> lookup 212 playersNothing
  • 1 pacific = [("Fs", [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")]),2 ("SH", [(3, "Matsunaka"), (21, "Wada"), (47, "Sugiuchi")]),3 ("Es", [(18, "Tanaka"), (21, "Iwakuma"), (32, "Matsui")])]45 lookupName :: String -> Int -> Maybe String6 lookupName t n = case (lookup t pacific) of7 Just players -> lookup n players8 Nothing -> Nothing> lookupName "Fs" 41Just "Inaba" …
  • -- ( )class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m ainstance Monad Maybe where return = Just fail = Nothing Nothing >>= f = Nothing (Just x) >>= f = f x Nothing Maybe Nothing >>= Int -> ? = Just Maybe Maybe Int >>= Int -> ? = ?
  • 1 pacific = [("Fs", [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")]), 2 ("SH", [(3, "Matsunaka"), (21, "Wada"), (47, "Sugiuchi")]), 3 ("Es", [(18, "Tanaka"), (21, "Iwakuma"), (32, "Matsui")])] 4 5 lookupName :: String -> Int -> Maybe String 6 lookupName t n = case (lookup t pacific) of 7 Just players -> lookup n players 8 Nothing -> Nothing 910 lookupName t n = lookup t pacific >>= lookup n> lookupName "Es" 18Just "Tanaka" [(Int, b)] -> Maybe b> lookupName "Fs" 18Nothing Maybe [(Int, String)]> lookupName "DH" 21Nothing -- (>>=) :: m a -> (a -> m b) -> m b lookup :: Eq a => a -> [(a, b)] -> Maybe b
  • Maybe Maybe Maybe Mayb a >>= a -> b >>= b -> c >>= c -> d
  • IO() String -> IO()main = putStrLn "Hello, World!"main = let a = putStrLn "Hello" in (a >> a) IO a
  • IO String String -> IO()main = getContents >>= putStrLn IO IO IO() = Str >>= Str -> ()