Advertisement
Advertisement

More Related Content

Advertisement
Advertisement

Haskell. Getting started (RU)

  1. Haskell И н с т р у к ц и я п о п р и г о т о в л е н и ю « Д о к а з а т е л ь с т в о - э т о п р о г р а м м а , а д о к а з ы в а е м а я ф о р м у л а - э т о т и п п р о г р а м м ы » Х . К а р р и
  2. Что такое функциональное программирование? http://ru.wikipedia.org/wiki/ Функциональное_программирование
  3. Haskell Функциональный С поддержкой отложенных вычислений Типизация строгая, статическая
  4. Где взять? http://hackage.haskell.org/platform/
  5. Hello world? main = putStrLn "hello, world" 
  6. Compile ghc --make hello.hs
  7. Haskell Console ghci
  8. Алгебра ghci> 2 + 15   17   ghci> 49 * 100   4900   ghci> 1892 - 1472   420   ghci> 5 / 2   2.5   ghci> 50 * 100 - 4999   1   ghci> 50 * (100 - 4999)   -244950  
  9. Логические операции ghci> 5 == 5   ghci> True && False True   False ghci> 1 == 0   ghci> True && True False   True ghci> 5 /= 5   ghci> False || True False   True ghci> 5 /= 4   ghci> not False   False   True ghci> "hello" == "hello" True
  10. Syntactic sugar ghci> succ 8   9 ghci> min 3.4 3.2   3.2   ghci> max 100 101   101  ghci> succ 9 + max 5 4 + 1   16   ghci> ( succ 9 ) + ( max 5 4 ) + 1   16   ghci> div 92 10 9 ghci> 92 `div` 10 9
  11. Baby function baby.hs
  12. Baby function baby.hs doubleMe x = x + x  
  13. Baby function baby.hs doubleMe x = x + x   ghci> :l baby   ghci> doubleMe 9 ghci> doubleMe 8.9
  14. Try yourself doubleUs x y = x*2 + y*2  
  15. Try yourself doubleUs x y = x*2 + y*2   Main> doubleUs 2 3 Main> 2 `doubleUs` 3
  16. Lists ghci> 1 : [] ghci> 1 : 2 : []
  17. Lists ghci> 1 : [] ghci> 1 : 2 : [] Syntactic sugar ;) ghci> let a = [1,2] ghci> a
  18. Lists ghci> 1 : [] ghci> 1 : 2 : [] Syntactic sugar ;) ghci> ['h','e','l','l','o'] ghci> let a = [1,2] ghci> a
  19. Lists Lists are a homogenous structure! ghci> ['h',1,'3','b']
  20. Lists ghci> [1,2,3,2] !! 2 3 ghci> [1,2,3,2] ++ [2,3] ghci> [2,4] : [[1,2,3,2]] [1,2,3,2,2,3] [[2,4],[1,2,3,2]] ghci> [3,9,1] > [2,1,0] ghci> 2 : [1,2,3,2] True [2,1,2,3,2]
  21. Lists Получение элементов списка ghci> let a = [3,2,5,7,9,1] ghci> head a ghci> tail a ghci> init a ghci> last a
  22. Try it yourself length <list> null <list> reverse <list> take <int> <list> drop <int> <list> maximum <list> minimum <list> sum <list> product <list> <int> `elem` <list>
  23. Ranges ghci> [1..15] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] ghci> ['a'..'z'] "abcdefghijklmnopqrstuvwxyz" ghci> ['K'..'Z'] "KLMNOPQRSTUVWXYZ"
  24. Ranges ghci> [2,4..20] ghci> [3,6..20] ghci> [0.1, 0.3 .. 1]
  25. Слово о лени [13,26..]
  26. Слово о лени ghci> take 24 [13,26..] [13,26,39,52,65,78,91,104,117,130,143,156,169,182,195,208,221, 234,247,260,273,286,299,312] ghci> take 10 (cycle [1,2,3]) [1,2,3,1,2,3,1,2,3,1] ghci> take 10 (repeat 5) [5,5,5,5,5,5,5,5,5,5]
  27. List comprehensions ghci> [x*2 | x <- [1..10]]
  28. List comprehensions ghci> [x*2 | x <- [1..10]] [2,4,6,8,10,12,14,16,18,20]
  29. List comprehensions let chunkyBacon xs = [ if odd x then "chunky" else "bacon" | x <- xs, x < 100, x /= 0 ]
  30. Do it yourself ghci> let adjectives = ["Maverick","Natty","Lucid","Oneiric"] ghci> let nouns = ["Lynx","Ocelot","Narwhal","Meerkat"] ["Maverick Meerkat","Natty Narwhal","Lucid Lynx","Oneiric Ocelot"] Help functions: filter (> 0) <list>
  31. Do it yourself ghci> let full_list = [ if (head adj) == (head noun) then adj ++ " " ++ noun else "" | adj <- adjectives, noun <- nouns] ghci> filter (/= "") full_list ["Maverick Meerkat","Natty Narwhal","Lucid Lynx","Oneiric Ocelot"
  32. Tuples Отличия кортежей от списков Кортежи могут быть неоднородны Кортежи имеют чётко заданную длину Кортежи разной длины имеют разные типы
  33. Tuples Неоднородность ("hello", "bacon", 515)
  34. Немного о типах ghci> :t 'a' 'a' :: Char ghci> :t [1,2,2] [1,2,2] :: Num t => [t] ghci> :t "hello" "hello" :: [Char] ghci> :t 2.5 2.5 :: Fractional a => a ghci> :t 1 1 :: Num a => a ghci> :t fst fst :: (a, b) -> a ghci> :t [1,2,2.5] [1,2,2.5] :: Fractional t => [t]
  35. Немного о типах ghci> :t [1,2] [1,2] :: Num t => [t] ghci> :t [1,2,3] [1,2,3] :: Num t => [t] ghci> :t ("hello", "bacon") ("hello", "bacon") :: ([Char], [Char]) ghci> :t ("hello", "bacon", 515) ("hello", "bacon", 515) :: Num t => ([Char], [Char], t)
  36. Lists of tuples ghci> :t [(1,2),(8,11),(4,5)] [(1,2),(8,11),(4,5)] :: (Num t1, Num t) => [(t, t1)] ghci> :t [(1,2),(8,11,9),(4,5)]
  37. Tuples functions ghci> fst (1,"sad") 1 ghci> snd (1,"sad") "sad" ghci> zip [1..] ["one", "two", "three", "four", "five"] [(1,"one"),(2,"two"),(3,"three"),(4,"four"),(5,"five")]
  38. Last do it yourself
  39. Last do it yourself Найти все возможные прямоугольные треугольники. (записать длины их сторон в кортежи) Максимально возможная длина стороны - 10
  40. Last do it yourself Решение: let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
  41. Использованная литература http://learnyouahaskell.com http://tryhaskell.org http://zvon.org/other/haskell/Outputglobal/ index.html
  42. Семинар подготовил: Зонов Кирилл Блог: http://graffzon.tumblr.com/ 2012г.

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
Advertisement