これは2.8.1のScala標準ライブラリのソースからのものです
/** Append linked list `that` at current position of this linked list
* @return the list after append (this is the list itself if nonempty,
* or list `that` if list this is empty. )
*/
def append(that: This): This = {
@tailrec
def loop(x: This) {
if (x.next.isEmpty) x.next = that
else loop(x.next)
}
if (isEmpty) that
else { loop(repr); repr }
}
/** Insert linked list `that` at current position of this linked list
* @note this linked list must not be empty
*/
def insert(that: This): Unit = {
require(nonEmpty, "insert into empty list")
if (that.nonEmpty) {
next = next.append(that)
}
}
この最後の行はすべきではありませんnext = that.append(next)
か?(つまり、このリンクリストの残りの部分を、挿入するリストの最後に配置しますか?
そうでない場合は、なぜですか?このコードは現在、挿入しているリストを現在のリストの最後に追加します。つまり、appendと同じです。