Masalah Fungsi foldRight() di Bahasa Scala

Sejak beberapa waktu lalu saya menemukan keanehan di fungsi bawaan Scala, foldRight. Berikut:

Pertama saya buat list berukuran kecil yang bernama babaPendek:

var babaPendek = 1 to 10 toList
babaPendek: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

Lalu saya buat list berukuran besar bernama babaPanjang:

var babaPanjang = 1 to 10000 toList
babaPanjang: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, ...

Kita cobakan babaPendek dengan foldLeft:

babaPendek.foldLeft("ah")(_+_)
res6: java.lang.String = ah12345678910

Lalu babaPanjang dengan foldRight:

babaPendek.foldRight("ah")(_+_)
res7: java.lang.String = 12345678910ah

Tidak ada masalah menggunakan fungsi foldLeft dan foldRight pada list babaPendek. Lalu kita cobakan fungsi foldLeft dan foldRight pada list babaPanjang:

babaPanjang.foldLeft("ah")(_+_)
res8: java.lang.String = ah12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229...

Ternyata sukses!

Lalu foldRight pada list babaPanjang:

babaPanjang.foldRight("ah")(_+_)
java.lang.StackOverflowError
at scala.collection.immutable.List.foldRight(List.scala:45)
at scala.collection.LinearSeqOptimized$class.foldRight(LinearSeqOptimized.scala:120)
at scala.collection.immutable.List.foldRight(List.scala:45)
at scala.collection.LinearSeqOptimized$class.foldRight(LinearSeqOptimized.scala:120)
at scala.collection.immutable.List.foldRight(List.scala:45)
at scala.collection.LinearSeqOptimized$class.foldRight(LinearSeqOptimized.scala:120)
.......................................
at scala.collection.immutable.List.foldRight(List.scala:45)
at scala.collection.LinearSeqOptimized$class.foldRight(LinearSeqOptimized.scala:120)

Ternyata foldRight ga bekerja pada list yang berukurang besar, gagal! Nggak tahu kenapa : D

Solusi dari saya untuk menggantikan fungsi foldRight pada list yang berukuran besar, gunakan:

babaPanjang.reverse.foldLeft("ah")(_+_)