2015年9月13日日曜日

開発環境

Learning Scala: Practical Functional Programming for the JVM (Jason Swartz (著)、O'Reilly Media)のPart Ⅰ. (Core Scala)、Chapter 5.(First-Class Functions)、Exercises 3.(No. 1874)を解いてみる。

その他参考書籍

Exercises 3.(No. 1874)

コード(Emacs)

import util.Random.nextInt

val f = (n:Int) => (x:Int) => n * x

for (n <- 0 to 10) {
  val g = f(n)
  for (x <- 0 to 10) {
    println(s"(f($n))($x) = g($x) = ${g(x)}")
  }
}

入出力結果(Terminal, REPL(Read, Eval, Print, Loop))

$ scala-2.11 sample3.scala
(f(0))(0) = g(0) = 0
(f(0))(1) = g(1) = 0
(f(0))(2) = g(2) = 0
(f(0))(3) = g(3) = 0
(f(0))(4) = g(4) = 0
(f(0))(5) = g(5) = 0
(f(0))(6) = g(6) = 0
(f(0))(7) = g(7) = 0
(f(0))(8) = g(8) = 0
(f(0))(9) = g(9) = 0
(f(0))(10) = g(10) = 0
(f(1))(0) = g(0) = 0
(f(1))(1) = g(1) = 1
(f(1))(2) = g(2) = 2
(f(1))(3) = g(3) = 3
(f(1))(4) = g(4) = 4
(f(1))(5) = g(5) = 5
(f(1))(6) = g(6) = 6
(f(1))(7) = g(7) = 7
(f(1))(8) = g(8) = 8
(f(1))(9) = g(9) = 9
(f(1))(10) = g(10) = 10
(f(2))(0) = g(0) = 0
(f(2))(1) = g(1) = 2
(f(2))(2) = g(2) = 4
(f(2))(3) = g(3) = 6
(f(2))(4) = g(4) = 8
(f(2))(5) = g(5) = 10
(f(2))(6) = g(6) = 12
(f(2))(7) = g(7) = 14
(f(2))(8) = g(8) = 16
(f(2))(9) = g(9) = 18
(f(2))(10) = g(10) = 20
(f(3))(0) = g(0) = 0
(f(3))(1) = g(1) = 3
(f(3))(2) = g(2) = 6
(f(3))(3) = g(3) = 9
(f(3))(4) = g(4) = 12
(f(3))(5) = g(5) = 15
(f(3))(6) = g(6) = 18
(f(3))(7) = g(7) = 21
(f(3))(8) = g(8) = 24
(f(3))(9) = g(9) = 27
(f(3))(10) = g(10) = 30
(f(4))(0) = g(0) = 0
(f(4))(1) = g(1) = 4
(f(4))(2) = g(2) = 8
(f(4))(3) = g(3) = 12
(f(4))(4) = g(4) = 16
(f(4))(5) = g(5) = 20
(f(4))(6) = g(6) = 24
(f(4))(7) = g(7) = 28
(f(4))(8) = g(8) = 32
(f(4))(9) = g(9) = 36
(f(4))(10) = g(10) = 40
(f(5))(0) = g(0) = 0
(f(5))(1) = g(1) = 5
(f(5))(2) = g(2) = 10
(f(5))(3) = g(3) = 15
(f(5))(4) = g(4) = 20
(f(5))(5) = g(5) = 25
(f(5))(6) = g(6) = 30
(f(5))(7) = g(7) = 35
(f(5))(8) = g(8) = 40
(f(5))(9) = g(9) = 45
(f(5))(10) = g(10) = 50
(f(6))(0) = g(0) = 0
(f(6))(1) = g(1) = 6
(f(6))(2) = g(2) = 12
(f(6))(3) = g(3) = 18
(f(6))(4) = g(4) = 24
(f(6))(5) = g(5) = 30
(f(6))(6) = g(6) = 36
(f(6))(7) = g(7) = 42
(f(6))(8) = g(8) = 48
(f(6))(9) = g(9) = 54
(f(6))(10) = g(10) = 60
(f(7))(0) = g(0) = 0
(f(7))(1) = g(1) = 7
(f(7))(2) = g(2) = 14
(f(7))(3) = g(3) = 21
(f(7))(4) = g(4) = 28
(f(7))(5) = g(5) = 35
(f(7))(6) = g(6) = 42
(f(7))(7) = g(7) = 49
(f(7))(8) = g(8) = 56
(f(7))(9) = g(9) = 63
(f(7))(10) = g(10) = 70
(f(8))(0) = g(0) = 0
(f(8))(1) = g(1) = 8
(f(8))(2) = g(2) = 16
(f(8))(3) = g(3) = 24
(f(8))(4) = g(4) = 32
(f(8))(5) = g(5) = 40
(f(8))(6) = g(6) = 48
(f(8))(7) = g(7) = 56
(f(8))(8) = g(8) = 64
(f(8))(9) = g(9) = 72
(f(8))(10) = g(10) = 80
(f(9))(0) = g(0) = 0
(f(9))(1) = g(1) = 9
(f(9))(2) = g(2) = 18
(f(9))(3) = g(3) = 27
(f(9))(4) = g(4) = 36
(f(9))(5) = g(5) = 45
(f(9))(6) = g(6) = 54
(f(9))(7) = g(7) = 63
(f(9))(8) = g(8) = 72
(f(9))(9) = g(9) = 81
(f(9))(10) = g(10) = 90
(f(10))(0) = g(0) = 0
(f(10))(1) = g(1) = 10
(f(10))(2) = g(2) = 20
(f(10))(3) = g(3) = 30
(f(10))(4) = g(4) = 40
(f(10))(5) = g(5) = 50
(f(10))(6) = g(6) = 60
(f(10))(7) = g(7) = 70
(f(10))(8) = g(8) = 80
(f(10))(9) = g(9) = 90
(f(10))(10) = g(10) = 100
$

0 コメント:

コメントを投稿