はじめての関数型プログラミング教室

 はじめての関数型プログラミング教室

書いて遊ぼうScalaハンズオン

3ea093e99fbc5ecf5b898c4c0ffd86c0?s=128

takahiro-hagino

May 26, 2015
Tweet

Transcript

  1. ͸͡Ίͯͷؔ਺ܕ ϓϩάϥϛϯάڭࣨ Hands-on Functional Programming in Scala

  2. ຊ೔࿩͢͜ͱ • ײ֮తʹཧղ͢Δؔ਺ܕϓϩάϥϛϯά • ίϨΫγϣϯͷ͜ͱ • ύλʔϯϚονͷ͜ͱ • ࠶ؼͷ͜ͱ

  3. ຊ೔࿩͞ͳ͍͜ͱ • Scalaͷ࣮༻తͳจ๏ • Ϋϩʔδϟ • ෦෼ద༻ • ΧϦʔԽ •

    ϞφυͳͲ
  4. ScalaΛಈ͔͢·Ͱ

  5. Mac OSΛ͓࢖͍ͷํ XCodeΛΠϯετʔϧ Command Line Developer ToolsΛΠϯετʔϧ $ xcode-select —install

    XCodeͷϥΠηϯεʹಉҙ $ sudo xcodebuild -license HomebrewΠϯετʔϧ $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com /Homebrew/install/master/install)” ScalaΠϯετʔϧ $ brew install scala
  6. Mac OSΛ͓࢖͍ͷํ XCodeΛΠϯετʔϧ Command Line Developer ToolsΛΠϯετʔϧ $ xcode-select —install

    XCodeͷϥΠηϯεʹಉҙ $ sudo xcodebuild -license HomebrewΠϯετʔϧ $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com /Homebrew/install/master/install)” ScalaΠϯετʔϧ $ brew install scala ͜Ε͚ͩ
  7. WindowsΛ͓࢖͍ͷํ • JDKΠϯετʔϧʢJava SEʣ
 http://www.oracle.com/technetwork/jp/java/javase/downloads/index.html • ScalaΠϯετʔϧ
 http://www.scala-lang.org/download/ • ؀ڥม਺Λઃఆ


    ίϯτϩʔϧύωϧʼγεςϜʼγεςϜͷৄࡉઃఆ
 Ϣʔβʔఆٛ؀ڥม਺ʹ৽ن௥Ճʢ໊લ͸PATHʹઃఆʣ
 JDKΛΠϯετʔϧͨ͠ϑΥϧμԼbin·Ͱͷύε;ʢʀͰ۠੾Δʣ
 ScalaΛΠϯετʔϧͨ͠ϑΥϧμԼͷbin·Ͱͷύε
  8. ؀ڥߏங͕໘౗͍͘͞ํ • ΦϯϥΠϯͷScala࣮ߦ؀ڥ͕͋Γ·͢ • tutorialspoint.com • paiza.io • codebunk.com •

    scastie.org
  9. ศརͳπʔϧ

  10. “Hello Scala!”

  11. REPL $ scala Welcome to Scala version 2.11.2 (Java HotSpot(TM)

    64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit
  12. REPL $ scala Welcome to Scala version 2.11.2 (Java HotSpot(TM)

    64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit λʔϛφϧ͔Β”scala”ίϚϯυΛ࣮ߦ
  13. REPL $ scala Welcome to Scala version 2.11.2 (Java HotSpot(TM)

    64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> println("Hello Scala!") Hello Scala! scala> :quit λʔϛφϧ͔Β”scala”ίϚϯυΛ࣮ߦ ऴྃ͢Δͱ͖͸”:quit”΋͘͠͸”:q”
  14. IDE object Main { def main(args: Array[String]) { println("Hello, Scala!")

    } }
  15. IDE object Main { def main(args: Array[String]) { println("Hello, Scala!")

    } } ͓·͡ͳ͍
  16. IDE object Main { def main(args: Array[String]) { println("Hello, Scala!")

    def hello(lang:String = "World"): String = "Hello, %s!".format(lang) println(hello("Scala")) } } ͓·͡ͳ͍ ͜Μͳ͔Μ͡ͰॲཧΛॻ͍͍͖ͯ·͢
  17. 15෼ͰֶͿ Scala

  18. ຊ೔ͷScalaจ๏͓͠ͳ͕͖ • ग़ྗ • ม਺ • ؔ਺Ϧςϥϧ • ίϨΫγϣϯ •

    ύλʔϯϚον
  19. ຊ೔ͷScalaจ๏͓͠ͳ͕͖ • ग़ྗ • ม਺ • ؔ਺Ϧςϥϧ • ίϨΫγϣϯ •

    ύλʔϯϚον ࠓ೔͸͜Ε͚ͩ
  20. Scalaͷ͍͍ͱ͜Ζ • ܕਪ࿦ (Type Inference) • ϓϨʔεϗϧμʔ • ౶ҥߏจ (Syntactic

    sugar) • ڧྗͳύλʔϯϚον • ҉໧ͷܕม׵ (Implicit Conversions)
  21. ScalaٯҾ͖Ϩγϐ ஛ఴ௚थʗౡຊଟՄࢠ ஶ

  22. ScalaٯҾ͖Ϩγϐ ஛ఴ௚थʗౡຊଟՄࢠ ஶ ৄ͘͠͸ͪ͜Β

  23. Scala ΫϨυ • var ېࢭ • mutable ېࢭ • while/for

    ϧʔϓ ېࢭ • null ېࢭ • Option.get ېࢭ • return ېࢭ • if ېࢭ (൒෼৑ஊ)
  24. Scala ΫϨυ • var ېࢭ • mutable ېࢭ • while/for

    ϧʔϓ ېࢭ • null ېࢭ • Option.get ېࢭ • return ېࢭ • if ېࢭ (൒෼৑ஊ) ࣾ಺Ͱ͸పఈͯ͠·͢
  25. ຊ೔ͷScalaจ๏͓͠ͳ͕͖ • ग़ྗ • ม਺ • ؔ਺Ϧςϥϧ • ίϨΫγϣϯ •

    ύλʔϯϚον
  26. ඪ४ग़ྗ scala> println("Hello, Scala!") Hello, Scala! scala> print("Hello, Scala!”) Hello,

    Scala!
  27. ඪ४ग़ྗ scala> println("Hello, Scala!") Hello, Scala! scala> print("Hello, Scala!”) Hello,

    Scala! վߦ΋Ұॹʹग़ྗ͞ΕΔ
  28. ຊ೔ͷScalaจ๏͓͠ͳ͕͖ • ग़ྗ • ม਺ • ؔ਺Ϧςϥϧ • ίϨΫγϣϯ •

    ύλʔϯϚον
  29. ม਺એݴ • val • ࠶୅ೖෆՄೳʢImmutableʣ • var • ࠶୅ೖՄೳʢMutableʣ

  30. ม਺એݴ scala> val lang = "scala" lang: String = scala

  31. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^
  32. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ ࠶୅ೖෆՄ
  33. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php ࠶୅ೖෆՄ
  34. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php scala> lang = "ruby" lang: String = ruby ࠶୅ೖෆՄ
  35. ม਺એݴ scala> val lang = "scala" lang: String = scala

    scala> lang = "java" <console>:8: error: reassignment to val lang = "java" ^ scala> var lang = "php" lang: String = php scala> lang = "ruby" lang: String = ruby ࠶୅ೖෆՄ ࠶୅ೖՄ
  36. ܕͷ͋Δม਺એݴ • val ม਺໊: ܕ = ஋ • ࠶୅ೖෆՄೳʢImmutableʣ •

    var ม਺໊: ܕ = ஋ • ࠶୅ೖՄೳʢMutableʣ
  37. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1
  38. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1
  39. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1 scala> val num0: Int = "1" <console>:7: error: type mismatch; found : String("1") required: Int val num0: Int = "1" ^
  40. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1 scala> val num0: Int = "1" <console>:7: error: type mismatch; found : String("1") required: Int val num0: Int = "1" ^ ܕ͕ҟͳΔ
  41. ܕͷ͋Δม਺એݴ scala> val num0: Int = 1 num0: Int =

    1 scala> val num0: Int = "1" <console>:7: error: type mismatch; found : String("1") required: Int val num0: Int = "1" ^ scala> val num0: String = "1" num0: String = 1 ܕ͕ҟͳΔ
  42. ຊ೔ͷScalaจ๏͓͠ͳ͕͖ • ग़ྗ • ม਺ • ؔ਺Ϧςϥϧ • ίϨΫγϣϯ •

    ύλʔϯϚον
  43. Ϧςϥϧͬͯͳʹʁ • ม਺ • ม਺͸ϓϩάϥϜͷϩδοΫͷதʹ஋Λ௚઀ॻ ͔ͣʹҰ౓୅ೖͨ͠΋ͷΛ࢖͏͜ͱ • Ϧςϥϧ • ϓϩάϥϜͷதʹ਺ࣈ΍จࣈͳͲͷ஋Λ


    ௚઀ॻ͍ͨఆ਺ͷ͜ͱ
  44. Ϧςϥϧͬͯͳʹʁ • ม਺ • ม਺͸ϓϩάϥϜͷϩδοΫͷதʹ஋Λ௚઀ॻ ͔ͣʹҰ౓୅ೖͨ͠΋ͷΛ࢖͏͜ͱ • Ϧςϥϧ • ϓϩάϥϜͷதʹ਺ࣈ΍จࣈͳͲͷ஋Λ


    ௚઀ॻ͍ͨఆ਺ͷ͜ͱ ରٛޠ
  45. Ϧςϥϧͬͯͳʹʁ scala> 12345 res0: Int = 12345 scala> "BizReach" res1:

    String = BizReach ੔਺Ϧςϥϧ จࣈྻϦςϥϧ
  46. ؔ਺Ϧςϥϧ • ؔ਺ΛҰ౓ม਺ʹ୅ೖͤͣʢ໊લΛ෇͚ͣʣ ʹϓϩάϥϜͷதʹ௚઀ॻ͘͜ͱ Y*OU Y 

  47. ؔ਺Ϧςϥϧ • ؔ਺ΛҰ౓ม਺ʹ୅ೖͤͣʢ໊લΛ෇͚ͣʣ ʹϓϩάϥϜͷதʹ௚઀ॻ͘͜ͱ Y*OU Y  ؔ਺ʹ౉͢஋

  48. ؔ਺Ϧςϥϧ • ؔ਺ΛҰ౓ม਺ʹ୅ೖͤͣʢ໊લΛ෇͚ͣʣ ʹϓϩάϥϜͷதʹ௚઀ॻ͘͜ͱ Y*OU Y  ؔ਺ʹ౉͢஋ ౉͢஋ͷܕ

  49. ؔ਺Ϧςϥϧ • ؔ਺ΛҰ౓ม਺ʹ୅ೖͤͣʢ໊લΛ෇͚ͣʣ ʹϓϩάϥϜͷதʹ௚઀ॻ͘͜ͱ Y*OU Y  ؔ਺ʹ౉͢஋ ౉͢஋ͷܕ

  50. ؔ਺Ϧςϥϧ • ؔ਺ΛҰ౓ม਺ʹ୅ೖͤͣʢ໊લΛ෇͚ͣʣ ʹϓϩάϥϜͷதʹ௚઀ॻ͘͜ͱ Y*OU Y  ؔ਺ʹ౉͢஋ ౉͢஋ͷܕ ؔ਺ͷॲཧͱ݁Ռ

  51. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2>
  52. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ
  53. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ
  54. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ
  55. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ ෳ਺ߦͷ৔߹
  56. ؔ਺Ϧςϥϧ scala> val plus = (x:Int, y:Int) => x +

    y plus: (Int, Int) => Int = <function2> scala> val sum = (x: Int, y: Int) => { | println("x is " + x) | println("y is " + y) | x + y | } sum: (Int, Int) => Int = <function2> scala> sum(2, 3) x is 2 y is 3 res4: Int = 5 ؔ਺ϦςϥϧΛ ม਺ʹ୅ೖ ෳ਺ߦͷ৔߹ ࠷ޙͷߦ͕ධՁ͞ΕΔ
  57. defΩʔϫʔυΛ࢖ͬͨؔ਺ఆٛ object Untitled { def main(args:Array[String]) = { def plus(x:

    Int, y: Int):Int = { x + y } } }
  58. defΩʔϫʔυΛ࢖ͬͨؔ਺ఆٛ object Untitled { def main(args:Array[String]) = { def plus(x:

    Int, y: Int):Int = { x + y } } } ໭Γ஋ͷܕ
  59. ຊ೔ͷScalaจ๏͓͠ͳ͕͖ • ग़ྗ • ม਺ • ؔ਺Ϧςϥϧ • ίϨΫγϣϯ •

    ύλʔϯϚον
  60. ίϨΫγϣϯ • List • Range • Tuple

  61. ίϨΫγϣϯ • List • Range • Tuple ࠓ೔͸͜Ε͚ͩ

  62. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3)
  63. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) جຊతͳListͷ࡞Γํ
  64. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() جຊతͳListͷ࡞Γํ
  65. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ
  66. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ
  67. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ
  68. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ
  69. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ Ϧετͷઌ಄ʹ௥Ճ͢Δ஋::ListͰ ৽͍͠ϦετΛͭ͘Γͬ·͢
  70. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) scala> 0 :: 1 :: 2 :: 3 :: Nil res2: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ Ϧετͷઌ಄ʹ௥Ճ͢Δ஋::ListͰ ৽͍͠ϦετΛͭ͘Γͬ·͢
  71. List scala> List(0, 1, 2, 3) res0: List[Int] = List(0,

    1, 2, 3) scala> Nil res1: scala.collection.immutable.Nil.type = List() scala> val ls: List[Int] = List(1, 2, 3) ls: List[Int] = List(1, 2, 3) scala> val ls0 = 0 :: ls ls0: List[Int] = List(0, 1, 2, 3) scala> 0 :: 1 :: 2 :: 3 :: Nil res2: List[Int] = List(0, 1, 2, 3) جຊతͳListͷ࡞Γํ ۭͷϦετͷ࡞Γํ ܕΛએݴͯ͠ม਺ʹ୅ೖ Ϧετͷઌ಄ʹ௥Ճ͢Δ஋::ListͰ ৽͍͠ϦετΛͭ͘Γͬ·͢ ۭͷϦετʹରͯ͠஋Λ௥Ճͨ͠Γ ෳ਺ͷ஋Λ::Ͱ௥Ճग़དྷ·͢
  72. Range scala> 1 to 10

  73. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) 1͔Β10·Ͱͷ੔਺ྻ
  74. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) 1͔Β10·Ͱͷ੔਺ྻ
  75. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) 1͔Β10·Ͱͷ੔਺ྻ 1͔Β10ະຬͷ੔਺ྻ
  76. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> 1 to 10 by 2 res2: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9) 1͔Β10·Ͱͷ੔਺ྻ 1͔Β10ະຬͷ੔਺ྻ
  77. Range scala> 1 to 10 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2,

    3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) scala> 1 to 10 by 2 res2: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9) 1͔Β10·Ͱͷ੔਺ྻ 1͔Β10ະຬͷ੔਺ྻ 1͔Β10·Ͱެࠩ2ͷ౳ࠩ਺ྻ
  78. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ)
  79. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) ஋ͷηοτͰ͢
  80. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) scala> ("Apple", "Orange", "Banana") res2: (String, String, String) = (Apple,Orange,Banana) ஋ͷηοτͰ͢ ෳ਺ͷ஋Λ·ͱΊΔ
  81. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) scala> ("Apple", "Orange", "Banana") res2: (String, String, String) = (Apple,Orange,Banana) scala> List(("Apple", "Orange"), ("Strawberry", “Cherry")) res3: List[(String, String)] = List((Apple,Orange), (Strawberry,Cherry)) ஋ͷηοτͰ͢ ෳ਺ͷ஋Λ·ͱΊΔ λϓϧͷϦετΛ࡞Δ
  82. Tuple scala> ("JAVA", "Eclipse") res0: (String, String) = (JAVA,Eclipse) scala>

    ("Scala", "IntelliJ") res1: (String, String) = (Scala,IntelliJ) scala> ("Apple", "Orange", "Banana") res2: (String, String, String) = (Apple,Orange,Banana) scala> List(("Apple", "Orange"), ("Strawberry", “Cherry")) res3: List[(String, String)] = List((Apple,Orange), (Strawberry,Cherry)) ஋ͷηοτͰ͢ ෳ਺ͷ஋Λ·ͱΊΔ λϓϧͷϦετΛ࡞Δ ܕ͸͜ͷΑ͏ʹͳΓ·͢
  83. ຊ೔ͷScalaจ๏͓͠ͳ͕͖ • ग़ྗ • ม਺ • ؔ਺Ϧςϥϧ • ίϨΫγϣϯ •

    ύλʔϯϚον
  84. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang

    = "Scala" lang match { case "Java" => println("Eclipse") case "Scala" => println("IntelliJ") case "PHP" | "Ruby"=> println("CodeRunner") case _ => println("Vim") } } }
  85. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang

    = "Scala" lang match { case "Java" => println("Eclipse") case "Scala" => println("IntelliJ") case "PHP" | "Ruby"=> println("CodeRunner") case _ => println("Vim") } } } “IntelliJ”͕ग़ྗ͞ΕΔ
  86. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang:String

    = "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } }
  87. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang:String

    = "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } } ஋Λฦ͢͜ͱ΋Ͱ͖·͢
  88. ύλʔϯϚον object Untitled { def main(args:Array[String]) = { val lang:String

    = "Scala" val ide:String = lang match { case "Java" => "Eclipse" case "Scala" => "IntelliJ" case "PHP" | "Ruby"=> "CodeRunner" case _ => "Vim" } println("Use "+ ide +".") } } ஋Λฦ͢͜ͱ΋Ͱ͖·͢ “Use IntelliJ.”͕ग़ྗ͞ΕΔ
  89. Functional Programming?

  90. ࣸ૾ͷΠϝʔδ • ͋Δू߹ͷཁૉͦΕͧΕʹ

  91. ࣸ૾ͷΠϝʔδ • ͋Δू߹ͷཁૉͦΕͧΕʹ • ԿΒ͔ͷޮՌΛద༻͢Δ͜ͱͰ

  92. ࣸ૾ͷΠϝʔδ • ͋Δू߹ͷཁૉͦΕͧΕʹ • ԿΒ͔ͷޮՌΛద༻͢Δ͜ͱͰ • ৽͍͠ू߹Λͭ͘Δ

  93. ࣸ૾ͷΠϝʔδ x1 x2 x3

  94. ࣸ૾ͷΠϝʔδ x1 x2 x3 function

  95. ࣸ૾ͷΠϝʔδ x1 x2 x3 y1 function

  96. ࣸ૾ͷΠϝʔδ x1 x2 x3 y1 function function

  97. ࣸ૾ͷΠϝʔδ x1 x2 x3 y1 y2 y3 function function

  98. ͨͱ͑͹SNSͰ ڞ௨ͷ༑ਓΛݟ͚ͭΔͱ͖

  99. ڞ௨ͷ༑ਓΛݟ͚ͭΔ ࣗ෼ͷ༑ਓ ༑ਓͷ༑ਓ

  100. ڞ௨ͷ༑ਓΛݟ͚ͭΔ ࣗ෼ͷ༑ਓ ༑ਓͷ༑ਓ

  101. ڞ௨ͷ༑ਓΛݟ͚ͭΔ ࣗ෼ͷ༑ਓ ༑ਓͷ༑ਓ

  102. A͘Μͱͷ ڞ௨ͷ༑ਓΛݟ͚ͭΔ ͢΂ͯͷ Ϣʔβʔ

  103. A͘Μͱͷ ڞ௨ͷ༑ਓΛݟ͚ͭΔ friends(me) ͢΂ͯͷ Ϣʔβʔ

  104. A͘Μͱͷ ڞ௨ͷ༑ਓΛݟ͚ͭΔ friends(me) ͢΂ͯͷ Ϣʔβʔ ࢲͷ༑ਓ

  105. A͘Μͱͷ ڞ௨ͷ༑ਓΛݟ͚ͭΔ friends(me) ͢΂ͯͷ Ϣʔβʔ friends(A) ࢲͷ༑ਓ

  106. A͘Μͱͷ ڞ௨ͷ༑ਓΛݟ͚ͭΔ friends(me) ͢΂ͯͷ Ϣʔβʔ friends(A) ࢲͷ༑ਓ

  107. A͘Μͱͷ ڞ௨ͷ༑ਓΛݟ͚ͭΔ friends(me) ͢΂ͯͷ Ϣʔβʔ friends(A) ࢲͷ༑ਓ ڞ௨ ͷ༑ਓ

  108. ؔ਺ܕϓϩάϥϛϯά • એݴܕϓϩάϥϛϯά • ؔ਺͸ୈҰڃΦϒδΣΫτ • λʢϥϜμʣ • ࢀরಁաੑ •

    ෭࡞༻ͷͳ͍ؔ਺ɺࣜ • ஗ԆධՁ • Ϟφυ
  109. ͜ΕΒͷཧ࿦ΛॳΊͯฉ͍ͯ ཧղͰ͖·͔͢ʁ

  110. ղͤ͵ ϞφυͬͯͳΜͩΑ(ʋ´ω`)

  111. ؔ਺ܕΛΠϝʔδ͢ΔͨΊʹ͸ • ߴߍͰͳΒͬͨू߹Λࢥ͍ग़͠·͠ΐ͏

  112. Collection API

  113. Collectionʢࣸ૾ʣΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce • filter • map

  114. Collectionʢࣸ૾ʣΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce • filter • map

  115. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP)
  116. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) ListΛͭ͘Γ·͢
  117. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( ) ListΛͭ͘Γ·͢
  118. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( x => println(x) ) ListΛͭ͘Γ·͢
  119. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( x => println(x) ) ListΛͭ͘Γ·͢ ؔ਺ϦςϥϧΛ౉͠·͢
  120. foreach scala> val langs = List("Java", "Scala", "PHP") langs: List[String]

    = List(Java, Scala, PHP) scala> langs.foreach ( x => println(x) ) Java Scala PHP ListΛͭ͘Γ·͢ ؔ਺ϦςϥϧΛ౉͠·͢ Listͷઌ಄͔ΒॱʹɺҾ਺ʹड͚औͬͨؔ਺Λద༻͠·͢
  121. Collectionʢࣸ૾ʣΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce • filter • map

  122. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10)
  123. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 1͔Β10·ͰͷListΛͭ͘Δ
  124. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( ) 1͔Β10·ͰͷListΛͭ͘Δ
  125. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( ) 1͔Β10·ͰͷListΛͭ͘Δ
  126. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ
  127. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  128. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  129. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  130. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢
  131. reduce scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> range.reduce( (x0, x1) => x0 + x1 ) res1: Int = 55 1͔Β10·ͰͷListΛͭ͘Δ Ҿ਺Λ2ͭऔΔؔ਺ϦςϥϧΛ౉͢ ઌ಄͔Βॱʹɺܭࢉ݁Ռͱ࣍ͷ஋Λ ౉͞Εͨؔ਺Ϧςϥϧʹద༻ͨ݁͠ՌΛฦ͢
  132. Collectionʢࣸ૾ʣΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce • filter • map

  133. filter scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  134. filter scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val odd = range.filter( )
  135. filter scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val odd = range.filter( x => x % 2 == 1 ) BooleanΛฦؔ͢਺ϦςϥϧΛ౉͢
  136. filter scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val odd = range.filter( x => x % 2 == 1 ) scala> val odd = range.filter( x => x % 2 == 1 ) odd: List[Int] = List(1, 3, 5, 7, 9) BooleanΛฦؔ͢਺ϦςϥϧΛ౉͢ ح਺ͷίϨΫγϣϯ͕࡞ΒΕͨ
  137. Collectionʢࣸ૾ʣΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce • filter • map

  138. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  139. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( )
  140. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( x => x * 2 )
  141. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( x => x * 2 ) ஋Λड͚औΓɺ৽͍͠஋Λฦؔ͢਺ϦςϥϧΛ౉͢
  142. map scala> val range = List(1, 2, 3, 4, 5,

    6, 7, 8, 9, 10) range: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> val twoTimes = range.map( x => x * 2 ) twoTimes: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20) ஋Λड͚औΓɺ৽͍͠஋Λฦؔ͢਺ϦςϥϧΛ౉͢ ΋ͱͷίϨΫγϣϯͷ஋ΛͦΕͧΕ2ഒͨ͠ίϨΫγϣϯ͕ͭ͘ΒΕͨ
  143. Practice!

  144. ࿅श໰୊ • 1͔Β100·Ͱͷ੔਺Λग़ྗ͢Δ • 1͔Β100·Ͱͷ੔਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷ੔਺ͰFizzBuzz

  145. ࿅श໰୊ • 1͔Β100·Ͱͷ੔਺Λग़ྗ͢Δ • 1͔Β100·Ͱͷ੔਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷ੔਺ͰFizzBuzz

  146. CollectionΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce, fold • filter •

    map
  147. 1͔Β100·Ͱͷ੔਺Λग़ྗ • ·ͣ͸͓޷͖ͳݴޠͰॻ͍ͯΈ͍ͯͩ͘͞

  148. 1͔Β100·Ͱͷ੔਺Λग़ྗ # PHP <?php for ($i = 1; $i <=

    100; $i++) { print $i . "\n"; } ?>
  149. 1͔Β100·Ͱͷ੔਺Λग़ྗ # Scala object Print { def main(args: Array[String]) {

    (1 to 100).foreach(println) } }
  150. 1͔Β100·Ͱͷ੔਺Λग़ྗ # Scala object Print { def main(args: Array[String]) {

    (1 to 100).foreach(println) } } x => println(x) ͷΑ͏ʹؔ਺͕ ஋ΛऔΔ͜ͱ͕໌Β͔ͳͱ͖͸লུՄ
  151. ࿅श໰୊ • 1͔Β100·Ͱͷ੔਺Λग़ྗ͢Δ • 1͔Β100·Ͱͷ੔਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷ੔਺ͰFizzBuzz

  152. CollectionΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce • filter • map

  153. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # PHP <?php for($i = 1, $sum = 0;

    $i <= 100; $i++) { $sum += $i; } echo($sum); ?>
  154. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (1 to 100).reduce((x0, x1) => x0 + x1) println(sum) } }
  155. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (1 to 100).reduce((x0, x1) => x0 + x1) println(sum) } } RangeΛ͔ͬ͜ͰғΜͰ࢖͑·͢
  156. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (x0: Int, x1: Int) => x0 + x1 val result = (1 to 100).reduce(sum) println(result) } }
  157. 1͔Β100ͷ੔਺Λͨ͠߹ΘͤΔ # Sclala object Sum { def main(args: Array[String]) {

    val sum = (x0: Int, x1: Int) => x0 + x1 val result = (1 to 100).reduce(sum) println(result) } } ؔ਺Λม਺ʹ୅ೖͯ͠ɺ ͜ΕΛ౉͢͜ͱ΋ग़དྷ·͢
  158. ࿅श໰୊ • 1͔Β100·Ͱͷ੔਺Λग़ྗ͢Δ • 1͔Β100·Ͱͷ੔਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷ੔਺ͰFizzBuzz

  159. CollectionΛૢ࡞͢Δ 4ͭͷϝιου • foreach • reduce • filter • map

  160. 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ # PHP <?php for ($i = 2, $sum =

    0; $i <= 100; $i += 2) { $sum += $i; } print($sum); ?>
  161. 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ # Scala object SumEven { def main(args: Array[String]) {

    val sum = (x0: Int, x1: Int) => x0 + x1 val isEven = (x: Int) => x % 2 == 0 // (1 to 100).filter(x => x % 2 == 0).reduce(_ + _) val result = (1 to 100).filter(isEven).reduce(sum) println(result) } } ίϨΫγϣϯϝιουΛνΣʔϯ͢Δ͜ͱ΋Ͱ͖·͢
  162. 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ # Scala object SumEven { def main(args: Array[String]) {

    val result = (2 to 100 by 2).sum println(result) } }
  163. 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ # Scala object SumEven { def main(args: Array[String]) {

    val result = (2 to 100 by 2).sum println(result) } } ίϨΫγϣϯʹ͸σϑΥϧτͰ ଟ͘ͷϝιου͕༻ҙ͞Ε͍ͯ·͢
  164. ࿅श໰୊ • 1͔Β100·Ͱͷ੔਺Λग़ྗ͢Δ • 1͔Β100·Ͱͷ੔਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷۮ਺Λͨ͠߹ΘͤΔ • 1͔Β100·Ͱͷ੔਺ͰFizzBuzz

  165. FizzBuzzͷϧʔϧ • 1͔Β100·Ͱͷ੔਺Λग़ྗ͢Δ • ͨͩ͠3ͷഒ਺ͷͱ͖͸Fizz • 5ͷഒ਺ͷͱ͖͸Buzz • 3ͱ5ͷഒ਺ͷͱ͖͸FizzBuzz •

    ਺ࣈΛจࣈྻʹஔ͖͔͑ͯग़ྗ͢Δ
  166. FizzBuzzͷΠϝʔδ • 1, 2, Fizz, 4, Buzz, Fizz, 7, 8,

    Fizz, Buzz,
 11, Fizz, 13, 14, FizzBuzz, …
  167. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # PHP <?php for ($i = 0; $i <

    100; $i ++) { if (($i % 3) === 0 && ($i % 5) === 0) { print("FizzBuzz".","); } else if($i % 3 === 0) { print("Fizz".","); } else if ($i % 5 === 0) { print("Buzz".","); } else { print($i.","); } } ?>
  168. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } }
  169. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } xΛ3Ͱׂͬͨ͋·Γͱɺ 5Ͱׂͬͨ͋·ΓͷTuple
  170. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } xΛ3Ͱׂͬͨ͋·Γͱɺ 5Ͱׂͬͨ͋·ΓͷTuple Tupleͷ஋ͰύλʔϯϚον ͢Δ͜ͱ΋Ͱ͖Δ
  171. 1͔Β100·Ͱͷ੔਺ͰFizzBuzz # Scala object FizzBuzz { def main(args:Array[String]) = {

    val fizzBuzz = (x: Int) => ( x%3, x%5 ) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => x.toString } val result = (1 to 100).map(fizzBuzz).mkString(",") println(result) } } fizzBuzzม਺ʹ౉͞Ε͍ͯΔ ͷ͸ؔ਺Ϧςϥϧ
  172. Recursive

  173. ࠶ؼͷ͸ͳ͠ • ͨͱ͑͹ɺ͋Δؔ਺͕ͦͷهड़ͷͳ͔Ͱ
 ࣗ෼ࣗ৴Λؔ਺ͱͯ͠ݺͼग़͢͜ͱɻ
 
 def recursive = {
 …


    recursive(xx)
 }
  174. ຤ඌ࠶ؼ • ؔ਺ͷఆٛͰɺ࠶ؼΛ༻͍Δͱ͖͸ɺ
 ࣗ෼ࣗ਎ͷݺͼग़͠͸ɺ
 ؔ਺ఆٛͷͷ࠷ޙʹ͢Δɻ • ຤ඌ࠶ؼҎ֎ͷ࠶ؼͰ͸ɺ
 ࠶ؼݺͼग़͠ͷͨͼʹελοΫΛͭ͘ΔͨΊ
 ΦʔόʔϑϩʔΛى͜͢ةݥ͕͋Γ·͢ɻ

  175. ֊৐ܭࢉͷྫ • ࣗવ਺ n ͷ֊৐ެࣜ
 n! = n * (n

    - 1) * … * 1 • ͜ΕΛܭࢉ͢Δؔ਺Λఆٛ͢Δ
 ʢ 0! = 1 ʣ
  176. ֊৐ܭࢉͷྫ object Untitled { def main(args:Array[String]) = { def factorial(n:Int):Int

    = if (n < 1) 1 else (n * factorial(n - 1)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } }
  177. ֊৐ܭࢉͷྫ object Untitled { def main(args:Array[String]) = { def factorial(n:Int):Int

    = if (n < 1) 1 else (n * factorial(n - 1)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } } ຤ඌ࠶ؼ͡Όͳ͍
  178. ֊৐ܭࢉʢ຤ඌ࠶ؼʣ object Untitled { def main(args:Array[String]) = { def factorial(n:Int,

    acc:Int = 1):Int = if (n < 1) acc else (factorial(n - 1, n * acc)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } }
  179. ֊৐ܭࢉʢ຤ඌ࠶ؼʣ object Untitled { def main(args:Array[String]) = { def factorial(n:Int,

    acc:Int = 1):Int = if (n < 1) acc else (factorial(n - 1, n * acc)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } } ΞΩϡϜϨʔλͷಋೖ
  180. ֊৐ܭࢉʢ຤ඌ࠶ؼʣ object Untitled { def main(args:Array[String]) = { def factorial(n:Int,

    acc:Int = 1):Int = if (n < 1) acc else (factorial(n - 1, n * acc)) println(factorial(1)) // 1 println(factorial(2)) // 2 println(factorial(3)) // 6 println(factorial(4)) // 24 } } ΞΩϡϜϨʔλͷಋೖ ͜ΕͰ຤ඌ࠶ؼʹͳͬͨ
  181. Practice!

  182. ϑΟϘφον਺ • n ൪໨ͷϑΟϘφον਺ΛF_nͰද͢ͱ • F_0 = 0 • F_1

    = 1 • F_n+2 = F_n + F_n+1 (n >= 0)
  183. ϑΟϘφον਺ྻͷΠϝʔδ • 0, 1, 1, 2, 3, 5, 8, 13,

    21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, …
  184. ϑΟϘφον਺ͷ໰୊ • ϑΟϘφον਺ͷ n ൪໨ͷ஋Λ
 ٻΊΔؔ਺Λͭ͘Δ
 
 fib(0) = 0


    fib(1) = 1
 fib(2) = 1
 fib(3) = 2
  185. ϑΟϘφον਺ͷྫ object Fibonacci { def main(args:Array[String]) = { def fib(n:

    Int): Int = if(n < 1) 0 else n match { case 1 => 1 case 2 => 1 case x => fib(x-1) + fib(x-2) } val fibString = (1 to 10).map(fib).mkString(", ") println(fibString) // 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 } } ຤ඌ࠶ؼ͡Όͳ͍
  186. ϑΟϘφον਺ʢ຤ඌ࠶ؼʣ object Fibonacci { def main(args:Array[String]) = { def fibList(lim:

    Int): List[Int] = { def fib0(lim: Int, fls: List[Int]): List[Int] = { if (lim < 1) fls else { fls match { case Nil => fib0(lim-1, 1 :: Nil) case x0 :: Nil => fib0(lim-1, 1 :: fls) case x1 :: x0 :: ls => fib0(lim-1, (x0 + x1) :: x1 :: x0 :: ls) } } } fib0(lim, Nil).reverse } def fib(n: Int): Int = fibList(n).last } } fibListͷ಺෦ʹؔ਺Λ࡞Δ fib0ͷͳ͔Ͱ͸ٯॱͰListΛ͍ͭͬͯ͘ΔͨΊɺ࠷ޙʹ໭͢
  187. ϑΟϘφον਺ྻʢ lazy val ʣ val fibList = (lim: Int) =>

    { lazy val fib0:(Int, List[Int]) => List[Int] = (lim:Int, fls:List[Int]) => { if (lim < 1) fls else { fls match { case Nil => fib0(lim-1, 1 :: Nil) case x0 :: Nil => fib0(lim-1, 1 :: fls) case x1 :: x0 :: ls => fib0(lim-1, (x0 + x1) :: x1 :: x0 :: ls) } } } fib0(lim, Nil).reverse } val fib = (n: Int) => fibList(n).last .lastOption.getOrElse(0)
  188. ͓͠·͍