Successfully reported this slideshow.
Upcoming SlideShare
×

# 関数プログラミングことはじめ revival

429 views

Published on

Scala 関西 Summit 2017 のハンズオンで使用した資料

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### 関数プログラミングことはじめ revival

1. 1. revival Scala Summit 2017 https://goo.gl/iKyEKs
2. 2.
3. 3. Scala
4. 4. Scala ?
5. 5. ?
6. 6. •Scala • • Scala ?
7. 7. • • • •
8. 8. • • • •
9. 9. f(x) = x + 1 Scala • = ( ) • Which is better? def f(x: Int) {x + 1} def f(x: Int) = x + 1 def f(x: Int) = return x + 1 ※ : 2
10. 10. ( ) • (mutable) • •
11. 11. ? int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum;
12. 12. https://goo.gl/iKyEKs
13. 13. • 1 n • int f(int n) { int total = 0; for (int i = 1; i <= n; i++) { total += i; } return total; }
14. 14. f(1) = 1 f(2) = 1 + 2 f(3) = 1 + 2 + 3 ... f(n) = 1 + 2 + 3 + ... + n
15. 15. f(1) = 1 f(2) = f(1) + 2 f(3) = f(2) + 3 ... f(n) = f(n - 1) + n f(1) = 1 f(n) = f(n - 1) + n
16. 16. def f(n: Int): Int = if (n == 1) 1 else f(n - 1) + n f(1) = 1 f(n) = f(n - 1) + n
17. 17. f(0) = 1 f(1) = 1 f(2) = 2 * 1 f(3) = 3 * 2 * 1 ... f(n) = n * ... * 3 * 2 * 1
18. 18. f(0) = 1 f(1) = 1 * f(0) f(2) = 2 * f(1) f(3) = 3 * f(2) ... f(n) = n * f(n - 1)
19. 19. f(0) = 1 f(n) = n * f(n - 1) def f(n: Int): Int = if (n == 0) 1 else n * f(n - 1)
20. 20. n ? 1, 1, 2, 3, 5, 8, 13, …
21. 21. f(0) = 0 f(1) = 1 f(2) = 0 + 1 f(3) = 1 + 1 f(4) = 1 + 2 f(5) = 2 + 3 ...
22. 22. f(0) = 0 f(1) = 1 f(2) = f(0) + f(1) f(3) = f(1) + f(2) f(4) = f(2) + f(3) f(5) = f(3) + f(4) ... f(n) = f(n - 2) + f(n - 1)
23. 23. f(0) = 0 f(1) = 1 f(n) = f(n - 2) + f(n - 1) def f(n: Int): Int = if (n == 0) 0 else if (n == 1) 1 else f(n - 2) + f(n - 1)
24. 24. sum def sum(ints: List[Int]): Int
25. 25. • Nil •head tail head :: tail 3 Nil Nil:: 3 :: Nil2 :: 2 :: 3 :: Nil1 :: • •
26. 26.
27. 27. Nil Nil::8 8 :: Nil::2 2 :: 8 :: Nil::1 1 :: 2 :: 8 :: Nil::5 5 :: 1 :: 2 :: 8 :: Nil
28. 28. sum(5 :: 1 :: 2 :: 8 :: Nil) sum( )  = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = 0Nil 8 :: Nil 2 :: 8 :: Nil 5 :: 1 :: 2 :: 8 :: Nil Nil8 8 :: Nil2 2 :: 8 :: Nil1 5 1 :: 2 :: 8 :: Nil 1 :: 2 :: 8 :: Nil
29. 29. sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = + sum( ) sum( ) = 0Nil 8 :: Nil 2 :: 8 :: Nil Nil8 8 :: Nil22 :: 8 :: Nil 1 head head tail head tail head tail head :: tail 1 :: 2 :: 8 :: Nil tail
30. 30. sum(Nil) = 0 sum(head :: tail) = head + sum(tail) def sum(list: List[Int]): Int = if (list.isEmpty) 0 else list.head + sum(list.tail) def sum(list: List[Int]): Int = list match { case Nil => 0 case head :: tail => head + sum(tail) }
31. 31. product def product(ints: List[Int]): Int
32. 32. max def max(ints: List[Int]): Int
33. 33. reverse def reverse(ints: List[Int]): List[Int]
34. 34. length def length(ints: List[Int]): Int
35. 35. def sum(list: List[Int]): Int = list match { case Nil => 0 case head :: tail => head + sum(tail) } def sum(list: List[Int]): Int = { def loop(acc: Int, l: List[Int]): Int = l match { case Nil => acc case head :: tail => loop(acc + head, tail) } loop(0, list) }
36. 36. • Functional Programming Principles in Scala  Scala Martin Odersky   https://www.coursera.org/course/progfun •   OCaml   http://www.amazon.co.jp/dp/4781911609
37. 37. • Scala & ― Scalaz   2   http://www.amazon.co.jp/dp/4844337769