2015年11月29日日曜日

開発環境

  • 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-d.(No. 3195)を解いてみる。

その他参考書籍

Exercises 1-d.(No. 3195)

コード(Emacs)

def fibonacci_stream(i1: Int, i2: Int): Stream[Int] = {
  i1 #:: fibonacci_stream(i2, i1 + i2)
}

def iter(n: Int, s: Stream[Int]) : Option[Int] = {
  val h = s.head
  if (n > h) iter(n, s.tail)
  else if (n == h) Option(s.tail.head)
  else None
}
def fibNext(n: Int) : Option[Int] =  {
  val fibonacci_series = fibonacci_stream(0, 1)
  iter(n, fibonacci_series.tail)
}

println(fibNext(8))
println(fibNext(9))

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

$ scala-2.11 sample1_d.scala
Some(13)
None
$

0 コメント:

コメントを投稿