Last active
December 10, 2015 05:18
-
-
Save boxp/4386408 to your computer and use it in GitHub Desktop.
This code is test for scala.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object fibonacci { | |
def main(args: Array[String]) { | |
var fibonacci = List(1,1) | |
while(fibonacci.length < args(0).toInt) { | |
fibonacci = fibonacci :+ fibonacci.takeRight(2).sum | |
} | |
println(fibonacci.apply(args(0).toInt - 1)) | |
} | |
} |
なるほど、:+ではもとのリストに要素を加えるのではなく、もとのリストに要素を加えたものを返すだけなのですね。REPLで要素が加わったリストが返ってきたのを見て勘違いしていました^^;
下のシンタックスシュガーについても後でテストコードを書いてみようかと思います。どうもありがとうございます。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
もとのコードだと、新しい
List
作っても、代入せずに捨ててるので無限ループこれ
は、これ↓
のシンタックスシュガー。なんでそんなシンタックスシュガーがあるのかは、これ
http://d.hatena.ne.jp/xuwei/20110130/1296360331
の後半とか参照。