開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Scala (プログラミング言語)
Learning Scala: Practical Functional Programming for the JVM (Jason Swartz (著)、O'Reilly Media)のPart Ⅰ. (Core Scala)、Chapter 7.(More Collections)、Exercises 1-a.(No. 3180)を解いてみる。
その他参考書籍
Exercises 1-a.(No. 3180)
コード(Emacs)
def fibonacci(x : Int): List[Int] = {
x match {
case 0 => List(0)
case 1 => List(0, 1)
case _ => {
val buffer = List(0, 1).toBuffer
while (buffer.size < x) buffer += buffer.takeRight(2).sum
buffer.toList
}
}
}
for (x <- List(0, 1, 2, 3, 4, 10)) {
println(fibonacci(x))
}
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
$ scala-2.11 sample1_a.scala List(0) List(0, 1) List(0, 1) List(0, 1, 1) List(0, 1, 1, 2) List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34) $
0 コメント:
コメントを投稿