haskell超入門 part.1

Download Haskell超入門 Part.1

Post on 22-May-2015

2.069 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • 1. Nabe Twitter: @nabe256

2. Haskell 3. Haskell (by Wikipedia) 4. 5. 6. 7. 8. 9. qsort [] = []qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger wheresmaller = [a | a :qLeaving GHCi.$ 18. > 256256> 1+2*37> 7/23.5> 2^256 19. > 8/4/21.0> 2^3^2512> (2^3)^264 20. :type(:t )> :type 256256 :: Num a => a> :t 1+21+2 :: Num a => a> :t 3.53.5 :: Fractional a => a> :t 7/2 21. :info (:i )> :info (+)class (Eq a, Show a) => Num a where(+) :: a -> a -> a...-- Defined in GHC.Numinfixl 6 + 22. Haskell Haskell 23. > aa> HelloHello 24. > [1,2,3][1,2,3]> head [1,2,3]1> tail [1,2,3][2,3] 25. > [1,2,3,4,5] !! 23> take 3 [1,2,3,4,5][1,2,3]> drop 3 [1,2,3,4,5][4,5]> reverse [1,2,3,4,5][5,4,3,2,1] 26. > length [1,2,3,4,5]5> length (tail [1,2,3,4,5])4> [1,2,3] ++ [4,5][1,2,3,4,5] 27. : (cons)> 1:2[1,2]> 1:[2,3][1,2,3] 28. > [1,a]> [1,Hello] 29. > [a,b,c]abc 30. > (1,2)(1,2)> (1,2,3)(1,2,3) 31. > (1,a)(1,a)> (1,Hello)(1,Hello)> (1,a,"hello",[1,2,3])(1,a,"hello",[1,2,3]) 32. > fst (1,hello)1> snd (1,hello)hello 33. Prelude> putStrLn Hello, World!Hello, World! Hello, World! 34. putStrLn Hello, World! 35. Prelude> take 3 [1,2,3,4,5][1,2,3] 2 2 36. Prelude> take 3 (tail [1,2,3,4,5])[2,3,4] 37. 38. $ ghciPrelude> let main = putStrLn Hello, World! let 39. mainPrelude> mainHello, World! 40. 41. hello.hsmain = putStrLn Hello, World! let 42. hello.hs$ ghc --make hello.hs$ ./helloHello, World!$ 43. Haskell 44. Haskell 45. $ ghci hello.hsOk, modules loaded: Main.Prelude Main> mainHello, World!Prelude Main> :q$ 46. $ ghciPrelude> :load hello.hsOk, modules loaded: Main.Prelude Main> mainHello, World!Prelude Main> :q$ 47. main main 48. :reload 49. hello.hs> :load hello.hs> mainHello, World! 50. GHCIhello.hs$ cat hello.hsmain = putStrLn Hello, World!$ #$ cat hello.hsmain = putStrLn Hello, Haskell! 51. GHCI> :load hello.hs> mainHello, World!> :reload 52. GHCI> :load hello.hs> mainHello, World!> :reload> mainHello, Haskell! 53. 54. :type :info 55. Haskell 56. 1head> head [1,2,3]1> :t headhead :: [a] -> a 57. head :: [a] -> a 58. head :: [a] -> a 59. head :: [a] -> a 60. head :: [a] -> a 61. head :: [a] -> a a 62. head :: [a] -> a[a] 63. head :: [a] -> a[a] -> a[a]a 64. head :: [a] -> a[a] -> a[a]a 65. head :: [a] -> a[a] -> a 66. a> head [1,2,3]1 67. head :: [a] -> a[a] -> a 68. head :: [Num] -> Num[Num] -> Num 69. head :: [Num] -> Numhead[Num] -> Num 70. head :: [Num] -> Numhead[Num] -> Num 71. > head [1,2,3]1 72. > head [1,2,3] 1 73. > head [1,2,3] 1 74. 1(2)sum> sum [1,2,3,4,5]15> :t sumsum :: Num a => [a] -> a 75. sum :: Num a => [a] -> a 76. sum :: Num a => [a] -> a 77. sum :: Num a => [a] -> a sum 78. sum :: Num a => [a] -> a [a]a 79. sum :: Num a => [a] -> a a 80. sum :: Num a => [a] -> a 81. sum :: Num a => [a] -> a 82. > sum [1,2,3,4,5]15 83. > sum [1,2,3,4,5]15 84. 2(1)take> take 3 [1,2,3,4,5][1,2,3]> :t taketake :: Int -> [a] -> [a] 85. take :: Int -> [a] -> [a] 86. take :: Int -> [a] -> [a] 87. take :: Int -> [a] -> [a] 88. take :: Int -> ([a] -> [a]) 89. take :: Int -> ([a] -> [a])[a][a] 90. take :: Int -> ([a] -> [a])A 91. take :: Int -> ([a] -> [a])IntA 92. take :: Int -> ([a] -> [a])Int A 93. take :: Int -> ([a] -> [a])Int ([a][a]) 94. take :: Int -> [a] -> [a]Int[a][a] 95. take :: Int -> [a] -> [a]Int[a][a] 96. take :: Int -> [a] -> [a]Int[a][a] 97. 98. takeInt(2)take :: Int -> [a] -> [a]Int[a][a] 99. takeInt(2)(take 2) :: Int -> [a] -> [a] Int [a] [a] 100. takeInt(2)(take 2) :: Int -> [a] -> [a] Int [a] [a] 101. takeInt(2)(take 2) :: [a] -> [a] [a] [a] 102. (take 2)[a]([4,5,6])(take 2) :: [a] -> [a] [a] [a] 103. (take 2)[a]([4,5,6])(take 2 [4,5,6]) :: [a] -> [a] [a] [a] 104. (take 2)[a]([4,5,6])(take 2 [4,5,6]) :: [a] -> [a] [a] [a] 105. (take 2)[a]([4,5,6])(take 2 [4,5,6]) :: [a] [a] 106. (take 2)[a]([4,5,6])(take 2 [4,5,6]) = [4,5] [a] 107. (take 2)[a]([4,5,6])(take 2 [4,5,6]) = [4,5] [4,5] 108. (take 2)[a]([4,5,6])(take 2 [4,5,6]) = [4,5] = [4,5] 109. > take 2 [4,5,6][4,5] 110. > take 2[4,5,6] Int[4,5] 111. > take 2[4,5,6] Int [a][4,5] 112. > take 2[4,5,6] Int [a][4,5][a] 113. > take 2[4,5,6] Int [a][4,5][a] 114. GHCI 115. > :t taketake :: Int -> [a] -> [a]> :t take 2take 2 :: [a] -> [a]> :t take 2 [4,5,6]take 2 [4,5,6] :: Num a => [a]> take 2 [4,5,6][4,5] 116. 117. 118. 119. Haskell