関数型プログラミングの世界

11,370 views

Published on

関数型プログラミングで重要となる高階関数と遅延評価について紹介。時間の関係で、遅延評価については Why Functional Programming Matters とその関連 URL の紹介しかしていない。

Published in: Education, Technology
4 Comments
23 Likes
Statistics
Notes
  • > 末尾呼び出し最適化と遅延評価の機能があるものを関数型言語と呼ぶということでしょうか?
    末尾呼び出しの最適化は処理系の実装の問題ですから関数型’言語’の必須機能ではないです。評価機の問題です。
    関数型プログラミングで必要となる機能はスライドに書いてあるように、高階関数と遅延評価です。だから、私はこの二つを自然に扱える言語という意味で LISP を関数型言語だと考えていないし、スライドでもそのように分類していません。
    あくまでも私の解釈ですから、渡邊さんは御自身のご理解の下で御自由に解釈してください。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • > 私は素のLISPは関数型言語だと考えていません。
    素のLISPとは何をさしているのでしょうか?

    > ただし、LISP方言の一種である Scheme は末尾呼び出し最適化と遅延評価の機能が仕様化されていますから、最後の方の言語比較表では関数型言語として扱っています。
    つまり、末尾呼び出し最適化と遅延評価の機能があるものを関数型言語と呼ぶということでしょうか?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • 私は素のLISPは関数型言語だと考えていません。
    ただし、LISP方言の一種である Scheme は末尾呼び出し最適化と遅延評価の機能が仕様化されていますから、最後の方の言語比較表では関数型言語として扱っています。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • LISPって関数型言語じゃないんですかね?
    マルチパラダイム言語なので、確かに手続き型の特徴を併せ持ちますが、手続き型と関数型のどちらかに振り分けるとしたら関数型言語に分類される言語だと思うのです。
    たとえばJonesらはその論文[1]の中で'Some mostly-functional languages, such as Lisp or SML.'と言うようにLISPが関数型言語であることを示唆していたりしますが。

    [1] Simon L Peyton Jones, Philip Wadler, Imperative functional programming. 1992.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
11,370
On SlideShare
0
From Embeds
0
Number of Embeds
201
Actions
Shares
0
Downloads
60
Comments
4
Likes
23
Embeds 0
No embeds

No notes for slide
  • \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
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 関数型プログラミングの世界

    1. 1. Welcome to the world of the functional programming Kenta Murata 2011.06.11 #osc11doMonday, June 13, 2011 1
    2. 2. Monday, June 13, 2011 2
    3. 3. mrkn Ruby Rubyhttp://www.flickr.com/photos/koichiroo/5244581973/Monday, June 13, 2011 3
    4. 4. Monday, 12010 3 June 13, 2011 4
    5. 5. Welcome to the world of the functional programming Kenta Murata 2011.06.11 #osc11doMonday, June 13, 2011 5
    6. 6. Functional ProgrammingMonday, June 13, 2011 6
    7. 7. Imperative Programming Procedural ProgrammingMonday, June 13, 2011 7
    8. 8. Fortran Basic Cobol Algol C C++ Java LISPMonday, June 13, 2011 8
    9. 9. Monday, June 13, 2011 9
    10. 10. Functional ProgrammingMonday, June 13, 2011 10
    11. 11. High-order Functions Lazy EvaluationMonday, June 13, 2011 11
    12. 12. Functions as Building Blocks for constructing programshttp://www.flickr.com/photos/stevendepolo/5597111652Monday, June 13, 2011 12
    13. 13. Functions as Glue for bonding functionshttp://www.flickr.com/photos/pseudoego/5417919481Monday, June 13, 2011 13
    14. 14. A lot of programming languages available for functional programminghttp://www.flickr.com/photos/32712959@N07/3222623206Monday, June 13, 2011 14
    15. 15. Monday, June 13, 2011 15
    16. 16. Monday, June 13, 2011 16
    17. 17. Monday, June 13, 2011 17
    18. 18. High-order FunctionsMonday, June 13, 2011 18
    19. 19. Monday, June 13, 2011 19
    20. 20. Monday, June 13, 2011 20
    21. 21. Haskell [ ] e.g. [] [1, 2, 3] (:) e.g. [1] => 1 : [] [1, 2, 3] => 1 : 2 : 3 : []Monday, June 13, 2011 21
    22. 22. (:) LISP cons cons Haskell cons :: a -> [a] -> [a] -- cons x [] = [x] -- cons x1 (x2:xs) = x1 : (cons x2 xs)Monday, June 13, 2011 22
    23. 23. cons :: a -> [a] -> [a] -- cons x [] = [x] -- cons x1 (x2:xs) = x1 : (cons x2 xs) // C++ template <typename a> std::list<a> cons(a const& x, std::list<a> const& xs);Monday, June 13, 2011 23
    24. 24. cons :: a -> [a] -> [a] -- cons x [] = [x] -- cons x1 (x2:xs) = x1 : (cons x2 xs) e.g. cons 0 [1,2,3,4] => x1 0, x2 1, xs [2,3,4]Monday, June 13, 2011 24
    25. 25. (Int) sum sum :: [Int] -> Int sum [] = 0 sum (n:ns) = n + (sum ns)Monday, June 13, 2011 25
    26. 26. cons sum cons :: a -> [a] -> [a] cons x [] = [x] cons x1 (x2:xs) = x1 : (cons x2 xs) sum :: [Int] -> Int sum [] = 0 sum (n:ns) = n + (sum ns)Monday, June 13, 2011 26
    27. 27. cons x [] = [x] sum [] = 0 cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns)Monday, June 13, 2011 27
    28. 28. (Int) product product :: [Int] -> Int product [] = 1 product (n:ns) = n * product nsMonday, June 13, 2011 28
    29. 29. cons x [] = [x] sum [] = 0 product [] = 1 cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns) product (n :ns) = n * (product ns)Monday, June 13, 2011 29
    30. 30. (Bool) allTrue allTrue :: [Bool] -> Bool allTrue [] = True allTrue (b:bs) = b && allTrue nsMonday, June 13, 2011 30
    31. 31. (Bool) anyTrue anyTrue :: [Bool] -> Bool anyTrue [] = False anyTrue (b:bs) = b || anyTrue nsMonday, June 13, 2011 31
    32. 32. cons x [] = [x] sum [] = 0 product [] = 1 allTrue [] = True anyTrue [] = False cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns) product (n :ns) = n * (product ns) allTrue (b :bs) = b && (allTrue bs) anyTrue (b :bs) = b || (anyTrue bs)Monday, June 13, 2011 32
    33. 33. cons x [] = [x] sum [] = 0 product [] = 1 allTrue [] = True anyTrue [] = False cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns) product (n :ns) = n * (product ns) allTrue (b :bs) = b && (allTrue bs) anyTrue (b :bs) = b || (anyTrue bs)Monday, June 13, 2011 32
    34. 34. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) FalseMonday, June 13, 2011 33
    35. 35. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) FalseMonday, June 13, 2011 33
    36. 36. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) FalseMonday, June 13, 2011 33
    37. 37. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) FalseMonday, June 13, 2011 33
    38. 38. cons reduce append :: [a] -> [a] -> [a] append xs ys = reduce (:) ys xs append [1,2] [3,4] -> reduce (:) [3,4] [1,2] -> reduce (:) [3,4] (1:2:[]) -> 1 : (reduce (:) [3,4] (2:[])) -> 1 : 2 : (reduce (:) [3,4] []) -> 1 : 2 : [3,4] -> [1,2,3,4]Monday, June 13, 2011 34
    39. 39. Monday, June 13, 2011 35
    40. 40. Monday, June 13, 2011 36
    41. 41. Monday, June 13, 2011 37
    42. 42. Functions as Building Blocks for constructing programshttp://www.flickr.com/photos/stevendepolo/5597111652Monday, June 13, 2011 38
    43. 43. Functions as Glue for bonding functionshttp://www.flickr.com/photos/pseudoego/5417919481Monday, June 13, 2011 39
    44. 44. Monday, June 13, 2011 40
    45. 45. -- Haskell reduce (+) 0 [1..10] // C++ int ns[] = {1,2,3,4,5,6,7,8,9,10}; std::accumulate(ns, ns + 10, 0, &std::plus<int>); # Ruby [*1..10].inject(0, :+)Monday, June 13, 2011 41
    46. 46. Monday, June 13, 2011 42
    47. 47. Lazy EvaluationMonday, June 13, 2011 43
    48. 48. Monday, June 13, 2011 44
    49. 49. Monday, June 13, 2011 45
    50. 50. http://www.sampou.org/haskell/article/whyfp.htmlMonday, June 13, 2011 46
    51. 51. http://www.infoq.com/interviews/john-hughes-fpMonday, June 13, 2011 47
    52. 52. http://weblog.raganwald.com/2007/03/why-why-functional-programming-matters.htmlMonday, June 13, 2011 48
    53. 53. language evaluation pureness Haskell lazy pure Concurrent Clean lazy pure OCaml strict impure F# strict impure Scheme strict impureMonday, June 13, 2011 49
    54. 54. language evaluation pureness Haskell lazy pure Concurrent Clean lazy pure OCaml strict impure F# strict impure Scheme strict impureMonday, June 13, 2011 50
    55. 55. Monday, June 13, 2011 51
    56. 56. Monday, 12010 3 June 13, 2011 52
    57. 57. Functional ProgrammingMonday, June 13, 2011 53
    58. 58. A lot of programming languages available for functional programminghttp://www.flickr.com/photos/32712959@N07/3222623206Monday, June 13, 2011 54
    59. 59. Monday, June 13, 2011 55
    60. 60. High-order FunctionsMonday, June 13, 2011 56
    61. 61. Functions as Building Blocks for constructing programshttp://www.flickr.com/photos/stevendepolo/5597111652Monday, June 13, 2011 57
    62. 62. Functions as Glue for bonding functionshttp://www.flickr.com/photos/pseudoego/5417919481Monday, June 13, 2011 58
    63. 63. Lazy EvaluationMonday, June 13, 2011 59
    64. 64. Monday, June 13, 2011 60

    ×