SlideShare a Scribd company logo
Haskell

?
1 public class Counter {
2
3     private int count = 0;
4
5     /**
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 {
2
3     private int count = 0;
4
5     /**
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 }


                        (         )
Haskell
Concurrent Clean



     OCaml            Erlang
     Scala         Lisp(Scheme)
       F#
What's 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?
It's Haskell!
                            -- Autrijus Tang(    )
C++             Perl

Python                 Ruby

C#                            Java

PHP

Haskell
1   cond :: Bool
 2   cond = True
 3
 4   price :: Int
 5   price = 2800
 6
 7   pi :: Float
 8   pi = 3.141592653589793
 9
10   greeting :: String
11   greeting = "Hello, Haskell!"
12
13   letter :: Char
14   letter = 'C'
‘A’       ‘B’       ‘C’      ‘D’

1 bools = [True, False, True] :: [Bool]
2
3 -- "ABCD"
4 letters = ['A', 'B', 'C', 'D'] :: [Char]
5
6 --
7 empty = [] :: [Int]
8
9 --                1
10 notEmpty = [[]] :: [[Float]]
1 --
2 (False, 'A') :: (Bool, Char)
3
4 --        (          )
5 ("Name", True, 123) :: (String, Bool, Int)
6
7 --
 8 ('a', (False, 'b')) :: (Char, (Bool, Char))
 9
10 --
11 (['a', 'b', ‘c’], [False, True]) :: ([Char], [Bool])
12
13 --
14 [('a', False), ('b', True)] :: [(Char, Bool)]
1   double :: Int -> Int
2   double x = x + x
3
4   add :: (Int, Int) -> Int
5   add (x, y) = x + y
6
7   mult :: Int -> Int -> Int -> Int
8   mult x y z = x * y * z

> double 3
6
> add (1, 7)
8
> mult 7 8 9
504
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 10
10
11   mult30 :: Int -> Int
12   mult30 = mult 10 3

> mult10 3 8
240
> mult30 5
150
1   twice :: (Int -> Int) -> Int -> Int
2   twice f x = f (f x)
3                                     Int
4   isDigit :: Char -> Bool
                                        Int
5   isDigit c = c >= '0' && c <= '9'
6
7   -- map :: (a -> b) -> [a] -> [b]

> twice (*2) 3
12
> 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]
3


length :: [a] -> Int
map :: (a -> b) -> [a] -> [b]
> 1 + 2
3
> (*) 1.1 2.3        -- 1.1 * 2.3
2.53
> 10 `div` 2         -- div 10 2
5

 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 where
3    (==) :: a -> a -> Bool
4    (/=) :: a -> a -> Bool
5
6 --    MyType Eq
7 instance Eq MyType where
8   (MyType a) == (MyType a') = (a == a')
1 --          [λxy. x+y]
2   add x y = x + y
3   add' = x -> (y -> x + y)
4   add'' = x y -> x + y
5
6   --
7 -- const x y = x
 8 const :: a -> (b -> a)
 9 const x = _ -> x
10
11 --
> 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 -> Int
3   factorial 0 = 1
4   factorial n = n * factorial (n - 1)
5
6   --
7 product :: Num a => [a] -> a
8 product []     = 1
9 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 larger
4     where
5         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 4
2   even 0 = True          = {even       }
3   even n = odd (n - 1)
                            odd 3
4                          = {odd    }
5   odd :: Int -> Bool
                            even 2
6   odd 0 = False          = {even       }
7   odd n = even (n - 1)
                            odd 1
                           = {odd    }
> even 4
                            even 0
True                       = {even       }
> odd 4
                            True
False
1 --                                   !
2   summary :: [Integer] -> Integer
3   summary []     = 0
4   summary (n:ns) = n + summary ns
5
6   --               (         )
 7 summary' :: [Integer] -> Integer
 8 summary' xs = f xs 0
 9   where
10     f [] sum     = sum
11     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.1
tarai :: Int -> Int -> Int -> Int
                                           1.8 GHz Intel Core i7
tarai 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)
                             Haskell

private 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 [] c
10   translate   (x:xs) [y]    c = if x   == c then y else translate xs [y] c
11   translate   (x:xs) (y:ys) c = if x   == c then y else translate xs ys c
12
13   --
14   translateString :: String -> String -> String -> String
15   translateString set1 set2 str = map (translate set1 set2) str
16
17   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."
21
22   --
23 main :: IO    ()
24 main = (do    [set1,set2] <- getArgs
25               contents    <- hGetContents stdin
26               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 [] c
10   translate   (x:xs) [y]    c = if x   == c then y else translate xs [y] c
11   translate   (x:xs) (y:ys) c = if x   == c then y else translate xs ys c
12
13   --
14   translateString :: String -> String -> String -> String
15   translateString set1 set2 str = map (translate set1 set2) str
16
17   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."
21
22   --
23 main :: IO    ()
24 main = (do    [set1,set2] <- getArgs
25               contents    <- hGetContents stdin
26               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           = Nothing
11     Nothing >>= f = Nothing         Just       Nothing
12     (Just x) >>= f = f x             1

                                              Maybe Int
lookup :: Eq a => a -> [(a, b)] -> Maybe b

players = [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")]

> lookup 11 players
Just "Darvish"

> lookup 212 players
Nothing
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

> lookupName "Fs" 41
Just "Inaba"




                                                  …
--             (    )
class Monad m where
  (>>=) :: m a -> (a -> m b) -> m b
  return :: a -> m a

instance 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
 9
10   lookupName' t n = lookup t pacific >>= lookup n

> lookupName' "Es" 18
Just "Tanaka"                                          [(Int, b)] -> Maybe b

> lookupName' "Fs" 18
Nothing                           Maybe [(Int, String)]

> lookupName' "DH" 21
Nothing
                             --
                             (>>=) :: 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 ->   ()
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語

More Related Content

What's hot

お前は PHP の歴史的な理由の数を覚えているのか
お前は PHP の歴史的な理由の数を覚えているのかお前は PHP の歴史的な理由の数を覚えているのか
お前は PHP の歴史的な理由の数を覚えているのかKousuke Ebihara
 
Scalaの新規事業でScalaの未経験者をオンボーディング
Scalaの新規事業でScalaの未経験者をオンボーディングScalaの新規事業でScalaの未経験者をオンボーディング
Scalaの新規事業でScalaの未経験者をオンボーディングTatsuya Iwamatsu
 
Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料時響 逢坂
 
問合せ最適化インサイド
問合せ最適化インサイド問合せ最適化インサイド
問合せ最適化インサイドTakahiro Itagaki
 
"Simple Made Easy" Made Easy
"Simple Made Easy" Made Easy"Simple Made Easy" Made Easy
"Simple Made Easy" Made EasyKent Ohashi
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaWiem Zine Elabidine
 
PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)Satoshi Yamada
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of CompositionScott Wlaschin
 
Domain Modeling Made Functional (DevTernity 2022)
Domain Modeling Made Functional (DevTernity 2022)Domain Modeling Made Functional (DevTernity 2022)
Domain Modeling Made Functional (DevTernity 2022)Scott Wlaschin
 
ClojurianからみたElixir
ClojurianからみたElixirClojurianからみたElixir
ClojurianからみたElixirKent Ohashi
 
ラムダ計算入門
ラムダ計算入門ラムダ計算入門
ラムダ計算入門Eita Sugimoto
 
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方kwatch
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionLambert Lum
 
Ruby でつくる型付き Ruby
Ruby でつくる型付き RubyRuby でつくる型付き Ruby
Ruby でつくる型付き Rubymametter
 
Session管理とRailsのcookie store
Session管理とRailsのcookie storeSession管理とRailsのcookie store
Session管理とRailsのcookie storeKamimura Taichi
 
F#によるFunctional Programming入門
F#によるFunctional Programming入門F#によるFunctional Programming入門
F#によるFunctional Programming入門bleis tift
 
Dts x dicoding #4 memulai pemrograman kotlin
Dts x dicoding #4 memulai pemrograman kotlinDts x dicoding #4 memulai pemrograman kotlin
Dts x dicoding #4 memulai pemrograman kotlinAhmad Arif Faizin
 
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to CeleryIdan Gazit
 
NTUSTxTDOH - Pwn基礎 2015/12/27
NTUSTxTDOH - Pwn基礎 2015/12/27NTUSTxTDOH - Pwn基礎 2015/12/27
NTUSTxTDOH - Pwn基礎 2015/12/27Sheng-Hao Ma
 

What's hot (20)

お前は PHP の歴史的な理由の数を覚えているのか
お前は PHP の歴史的な理由の数を覚えているのかお前は PHP の歴史的な理由の数を覚えているのか
お前は PHP の歴史的な理由の数を覚えているのか
 
Scalaの新規事業でScalaの未経験者をオンボーディング
Scalaの新規事業でScalaの未経験者をオンボーディングScalaの新規事業でScalaの未経験者をオンボーディング
Scalaの新規事業でScalaの未経験者をオンボーディング
 
Ekmett勉強会発表資料
Ekmett勉強会発表資料Ekmett勉強会発表資料
Ekmett勉強会発表資料
 
問合せ最適化インサイド
問合せ最適化インサイド問合せ最適化インサイド
問合せ最適化インサイド
 
"Simple Made Easy" Made Easy
"Simple Made Easy" Made Easy"Simple Made Easy" Made Easy
"Simple Made Easy" Made Easy
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)PostgreSQL SQLチューニング入門 実践編(pgcon14j)
PostgreSQL SQLチューニング入門 実践編(pgcon14j)
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of Composition
 
Domain Modeling Made Functional (DevTernity 2022)
Domain Modeling Made Functional (DevTernity 2022)Domain Modeling Made Functional (DevTernity 2022)
Domain Modeling Made Functional (DevTernity 2022)
 
ClojurianからみたElixir
ClojurianからみたElixirClojurianからみたElixir
ClojurianからみたElixir
 
ラムダ計算入門
ラムダ計算入門ラムダ計算入門
ラムダ計算入門
 
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Ruby でつくる型付き Ruby
Ruby でつくる型付き RubyRuby でつくる型付き Ruby
Ruby でつくる型付き Ruby
 
Session管理とRailsのcookie store
Session管理とRailsのcookie storeSession管理とRailsのcookie store
Session管理とRailsのcookie store
 
F#によるFunctional Programming入門
F#によるFunctional Programming入門F#によるFunctional Programming入門
F#によるFunctional Programming入門
 
Dts x dicoding #4 memulai pemrograman kotlin
Dts x dicoding #4 memulai pemrograman kotlinDts x dicoding #4 memulai pemrograman kotlin
Dts x dicoding #4 memulai pemrograman kotlin
 
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to Celery
 
NTUSTxTDOH - Pwn基礎 2015/12/27
NTUSTxTDOH - Pwn基礎 2015/12/27NTUSTxTDOH - Pwn基礎 2015/12/27
NTUSTxTDOH - Pwn基礎 2015/12/27
 

Viewers also liked

5分で分かるgitのrefspec
5分で分かるgitのrefspec5分で分かるgitのrefspec
5分で分かるgitのrefspecikdysfm
 
JavaScriptの落とし穴
JavaScriptの落とし穴JavaScriptの落とし穴
JavaScriptの落とし穴ikdysfm
 
Fun with Social, Windows 8 and Javascript
Fun with Social, Windows 8 and JavascriptFun with Social, Windows 8 and Javascript
Fun with Social, Windows 8 and JavascriptJoris Poelmans
 
Building your first Windows Phone 7 application for SharePoint
Building your first Windows Phone 7 application for SharePointBuilding your first Windows Phone 7 application for SharePoint
Building your first Windows Phone 7 application for SharePointJoris Poelmans
 
The Connected Company - Event Anders Vergaderen
The Connected Company - Event Anders VergaderenThe Connected Company - Event Anders Vergaderen
The Connected Company - Event Anders VergaderenJoris Poelmans
 
Everything you always wanted to know about SharePoint 2013 Search relevance
Everything you always wanted to know about SharePoint 2013 Search relevanceEverything you always wanted to know about SharePoint 2013 Search relevance
Everything you always wanted to know about SharePoint 2013 Search relevanceJoris Poelmans
 
Intro to MUI and variations in SharePoint 2010
Intro to MUI and variations in SharePoint 2010Intro to MUI and variations in SharePoint 2010
Intro to MUI and variations in SharePoint 2010Joris Poelmans
 
Apps for Office Introduction
Apps for Office IntroductionApps for Office Introduction
Apps for Office IntroductionJoris Poelmans
 
Yammer Social Data Mining
Yammer Social Data MiningYammer Social Data Mining
Yammer Social Data MiningJoris Poelmans
 
Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...
Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...
Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...Joris Poelmans
 
Claim Based Authentication in SharePoint 2010 for Community Day 2011
Claim Based Authentication in SharePoint 2010 for Community Day 2011Claim Based Authentication in SharePoint 2010 for Community Day 2011
Claim Based Authentication in SharePoint 2010 for Community Day 2011Joris Poelmans
 
The future of business process apps - a Microsoft perspective
The future of business process apps - a Microsoft perspectiveThe future of business process apps - a Microsoft perspective
The future of business process apps - a Microsoft perspectiveJoris Poelmans
 
Exploring search driven applications with SharePoint 2013
Exploring search driven applications with SharePoint 2013Exploring search driven applications with SharePoint 2013
Exploring search driven applications with SharePoint 2013Joris Poelmans
 
What’s new on the Microsoft Azure Data Platform
What’s new on the Microsoft Azure Data Platform What’s new on the Microsoft Azure Data Platform
What’s new on the Microsoft Azure Data Platform Joris Poelmans
 
SharePoint Server 2013 : The big five
SharePoint Server 2013 : The big fiveSharePoint Server 2013 : The big five
SharePoint Server 2013 : The big fiveJoris Poelmans
 

Viewers also liked (16)

Petrobras cascade-chinook
Petrobras cascade-chinookPetrobras cascade-chinook
Petrobras cascade-chinook
 
5分で分かるgitのrefspec
5分で分かるgitのrefspec5分で分かるgitのrefspec
5分で分かるgitのrefspec
 
JavaScriptの落とし穴
JavaScriptの落とし穴JavaScriptの落とし穴
JavaScriptの落とし穴
 
Fun with Social, Windows 8 and Javascript
Fun with Social, Windows 8 and JavascriptFun with Social, Windows 8 and Javascript
Fun with Social, Windows 8 and Javascript
 
Building your first Windows Phone 7 application for SharePoint
Building your first Windows Phone 7 application for SharePointBuilding your first Windows Phone 7 application for SharePoint
Building your first Windows Phone 7 application for SharePoint
 
The Connected Company - Event Anders Vergaderen
The Connected Company - Event Anders VergaderenThe Connected Company - Event Anders Vergaderen
The Connected Company - Event Anders Vergaderen
 
Everything you always wanted to know about SharePoint 2013 Search relevance
Everything you always wanted to know about SharePoint 2013 Search relevanceEverything you always wanted to know about SharePoint 2013 Search relevance
Everything you always wanted to know about SharePoint 2013 Search relevance
 
Intro to MUI and variations in SharePoint 2010
Intro to MUI and variations in SharePoint 2010Intro to MUI and variations in SharePoint 2010
Intro to MUI and variations in SharePoint 2010
 
Apps for Office Introduction
Apps for Office IntroductionApps for Office Introduction
Apps for Office Introduction
 
Yammer Social Data Mining
Yammer Social Data MiningYammer Social Data Mining
Yammer Social Data Mining
 
Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...
Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...
Building search-driven Windows 8 and Windows Phone 8 apps for SharePoint Serv...
 
Claim Based Authentication in SharePoint 2010 for Community Day 2011
Claim Based Authentication in SharePoint 2010 for Community Day 2011Claim Based Authentication in SharePoint 2010 for Community Day 2011
Claim Based Authentication in SharePoint 2010 for Community Day 2011
 
The future of business process apps - a Microsoft perspective
The future of business process apps - a Microsoft perspectiveThe future of business process apps - a Microsoft perspective
The future of business process apps - a Microsoft perspective
 
Exploring search driven applications with SharePoint 2013
Exploring search driven applications with SharePoint 2013Exploring search driven applications with SharePoint 2013
Exploring search driven applications with SharePoint 2013
 
What’s new on the Microsoft Azure Data Platform
What’s new on the Microsoft Azure Data Platform What’s new on the Microsoft Azure Data Platform
What’s new on the Microsoft Azure Data Platform
 
SharePoint Server 2013 : The big five
SharePoint Server 2013 : The big fiveSharePoint Server 2013 : The big five
SharePoint Server 2013 : The big five
 

Similar to Haskellで学ぶ関数型言語

関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)riue
 
Useful javascript
Useful javascriptUseful javascript
Useful javascriptLei Kang
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisSpbDotNet Community
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016Naoki Kitora
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-stdPaul Phillips
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscationguest9006ab
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''OdessaJS Conf
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programmingLukasz Dynowski
 
関数プログラミングことはじめ revival
関数プログラミングことはじめ revival関数プログラミングことはじめ revival
関数プログラミングことはじめ revivalNaoki Kitora
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 

Similar to Haskellで学ぶ関数型言語 (20)

関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
 
Vcs16
Vcs16Vcs16
Vcs16
 
Scala kansai summit-2016
Scala kansai summit-2016Scala kansai summit-2016
Scala kansai summit-2016
 
Brief tour of psp-std
Brief tour of psp-stdBrief tour of psp-std
Brief tour of psp-std
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Monadologie
MonadologieMonadologie
Monadologie
 
Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''Christian Gill ''Functional programming for the people''
Christian Gill ''Functional programming for the people''
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
関数プログラミングことはじめ revival
関数プログラミングことはじめ revival関数プログラミングことはじめ revival
関数プログラミングことはじめ revival
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
Python Tidbits
Python TidbitsPython Tidbits
Python Tidbits
 
Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
R programming language
R programming languageR programming language
R programming language
 

Recently uploaded

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfDr. M. Kumaresan Hort.
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online PresentationGDSCYCCE
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxbennyroshan06
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxShajedul Islam Pavel
 

Recently uploaded (20)

NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 

Haskellで学ぶ関数型言語

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. ?
  • 8.
  • 9.
  • 10.
  • 11.
  • 12. 1 public class Counter { 2 3 private int count = 0; 4 5 /** 6 * . 7 * @param value 8 * @return 9 */ 10 public int up(int value) { 11 count += value; 12 return count; 13 } 14 }
  • 13. 1 public class Counter { 2 3 private int count = 0; 4 5 /** 6 * . 7 * @param value 8 * @return 9 */ 10 public int up(int value) { 11 count += value; 12 return count; 13 } 14 }
  • 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 } ( )
  • 15. 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 } ( )
  • 16. Haskell Concurrent Clean OCaml Erlang Scala Lisp(Scheme) F#
  • 17.
  • 18.
  • 19. What's 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? It's Haskell! -- Autrijus Tang( ) C++ Perl Python Ruby C# Java PHP Haskell
  • 20.
  • 21. 1 cond :: Bool 2 cond = True 3 4 price :: Int 5 price = 2800 6 7 pi :: Float 8 pi = 3.141592653589793 9 10 greeting :: String 11 greeting = "Hello, Haskell!" 12 13 letter :: Char 14 letter = 'C'
  • 22. ‘A’ ‘B’ ‘C’ ‘D’ 1 bools = [True, False, True] :: [Bool] 2 3 -- "ABCD" 4 letters = ['A', 'B', 'C', 'D'] :: [Char] 5 6 -- 7 empty = [] :: [Int] 8 9 -- 1 10 notEmpty = [[]] :: [[Float]]
  • 23. 1 -- 2 (False, 'A') :: (Bool, Char) 3 4 -- ( ) 5 ("Name", True, 123) :: (String, Bool, Int) 6 7 -- 8 ('a', (False, 'b')) :: (Char, (Bool, Char)) 9 10 -- 11 (['a', 'b', ‘c’], [False, True]) :: ([Char], [Bool]) 12 13 -- 14 [('a', False), ('b', True)] :: [(Char, Bool)]
  • 24. 1 double :: Int -> Int 2 double x = x + x 3 4 add :: (Int, Int) -> Int 5 add (x, y) = x + y 6 7 mult :: Int -> Int -> Int -> Int 8 mult x y z = x * y * z > double 3 6 > add (1, 7) 8 > mult 7 8 9 504
  • 25. 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 10 10 11 mult30 :: Int -> Int 12 mult30 = mult 10 3 > mult10 3 8 240 > mult30 5 150
  • 26. 1 twice :: (Int -> Int) -> Int -> Int 2 twice f x = f (f x) 3 Int 4 isDigit :: Char -> Bool Int 5 isDigit c = c >= '0' && c <= '9' 6 7 -- map :: (a -> b) -> [a] -> [b] > twice (*2) 3 12 > map (+1) [1, 2, 3, 4] [2, 3, 4, 5] > map isDigit ['a', '0', 'b', '9'] [False, True, False, True]
  • 27. > length [1, 3, 5, 7] 4 > length [True, False] 2 > length [length, length, length] 3 length :: [a] -> Int map :: (a -> b) -> [a] -> [b]
  • 28. > 1 + 2 3 > (*) 1.1 2.3 -- 1.1 * 2.3 2.53 > 10 `div` 2 -- div 10 2 5 1 (+) :: Num a => a -> a -> a 2 (*) :: Num a => a -> a -> a 3 negate :: Num a => a -> a 4 abs :: Num a => a -> a
  • 29. 1 -- ( Ord, Show, Read ) 2 class Eq a where 3 (==) :: a -> a -> Bool 4 (/=) :: a -> a -> Bool 5 6 -- MyType Eq 7 instance Eq MyType where 8 (MyType a) == (MyType a') = (a == a')
  • 30. 1 -- [λxy. x+y] 2 add x y = x + y 3 add' = x -> (y -> x + y) 4 add'' = x y -> x + y 5 6 -- 7 -- const x y = x 8 const :: a -> (b -> a) 9 const x = _ -> x 10 11 -- > map (x -> x * 2 + 1) [1, 3, 5, 7] [3, 7, 11, 15]
  • 31. > [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]
  • 32. 1 -- 2 factorial :: Int -> Int 3 factorial 0 = 1 4 factorial n = n * factorial (n - 1) 5 6 -- 7 product :: Num a => [a] -> a 8 product [] = 1 9 product (n:ns) = n * product ns 1 2 3 4 1:[2,3,4] 4:[]
  • 33. 1 qsort :: Ord a => [a] -> [a] 2 qsort [] = [] 3 qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger 4 where 5 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]
  • 34. 1 even :: Int -> Bool even 4 2 even 0 = True = {even } 3 even n = odd (n - 1) odd 3 4 = {odd } 5 odd :: Int -> Bool even 2 6 odd 0 = False = {even } 7 odd n = even (n - 1) odd 1 = {odd } > even 4 even 0 True = {even } > odd 4 True False
  • 35.
  • 36. 1 -- ! 2 summary :: [Integer] -> Integer 3 summary [] = 0 4 summary (n:ns) = n + summary ns 5 6 -- ( ) 7 summary' :: [Integer] -> Integer 8 summary' xs = f xs 0 9 where 10 f [] sum = sum 11 f (y:ys) sum = f ys (y + sum)
  • 37. 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
  • 38. 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 } !!
  • 39. 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]
  • 40. Mac OS X 10.7.1 tarai :: Int -> Int -> Int -> Int 1.8 GHz Intel Core i7 tarai 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) Haskell private 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
  • 41. Monad
  • 42. Haskell (※ )
  • 43.
  • 44.
  • 45.
  • 46.
  • 47. 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 [] c 10 translate (x:xs) [y] c = if x == c then y else translate xs [y] c 11 translate (x:xs) (y:ys) c = if x == c then y else translate xs ys c 12 13 -- 14 translateString :: String -> String -> String -> String 15 translateString set1 set2 str = map (translate set1 set2) str 16 17 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." 21 22 -- 23 main :: IO () 24 main = (do [set1,set2] <- getArgs 25 contents <- hGetContents stdin 26 putStr $ translateString set1 set2 contents) `catchError` usage
  • 48. 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 [] c 10 translate (x:xs) [y] c = if x == c then y else translate xs [y] c 11 translate (x:xs) (y:ys) c = if x == c then y else translate xs ys c 12 13 -- 14 translateString :: String -> String -> String -> String 15 translateString set1 set2 str = map (translate set1 set2) str 16 17 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." 21 22 -- 23 main :: IO () 24 main = (do [set1,set2] <- getArgs 25 contents <- hGetContents stdin 26 putStr $ translateString set1 set2 contents) `catchError` usage
  • 49. Monad
  • 50. 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 = Nothing 11 Nothing >>= f = Nothing Just Nothing 12 (Just x) >>= f = f x 1 Maybe Int
  • 51. lookup :: Eq a => a -> [(a, b)] -> Maybe b players = [(6, "Nakata"), (11, "Darvish"), (41, "Inaba")] > lookup 11 players Just "Darvish" > lookup 212 players Nothing
  • 52. 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 > lookupName "Fs" 41 Just "Inaba" …
  • 53. -- ( ) class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a instance Monad Maybe where return = Just fail = Nothing Nothing >>= f = Nothing (Just x) >>= f = f x Nothing Maybe Nothing >>= Int -> ? = Just Maybe Maybe Int >>= Int -> ? = ?
  • 54. 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 9 10 lookupName' t n = lookup t pacific >>= lookup n > lookupName' "Es" 18 Just "Tanaka" [(Int, b)] -> Maybe b > lookupName' "Fs" 18 Nothing Maybe [(Int, String)] > lookupName' "DH" 21 Nothing -- (>>=) :: m a -> (a -> m b) -> m b lookup :: Eq a => a -> [(a, b)] -> Maybe b
  • 55. Maybe Maybe Maybe Mayb a >>= a -> b >>= b -> c >>= c -> d
  • 56. IO() String -> IO() main = putStrLn "Hello, World!" main = let a = putStrLn "Hello" in (a >> a) IO a
  • 57. IO String String -> IO() main = getContents >>= putStrLn IO IO IO () = Str >>= Str -> ()

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n