natpryce
12/15/2017, 3:43 PMlet
, apply
, also
, etc. to work with tail recursive functions?
For example, I’d have thought the following two functions were the same, but the foo2
is not tail recursive.
tailrec fun foo1(acc: Int, x: Int?): Int? =
if (x == null) {
null
} else if (x <= 0) {
acc
} else {
foo1(acc+1, x-1)
}
tailrec fun foo2(acc: Int, x: Int?): Int? =
x?.let {
if (it <= 0) {
acc
} else {
foo2(acc+1, it-1)
}
}
pdvrieze
12/15/2017, 4:02 PM