Draft
Conversation
Contributor
|
Bad samples fun main() {
//sampleStart
// Non-tail-recursive factorial (less efficient, risk of stack overflow)
fun factorial(n: Long): Long =
if (n <= 1) 1
else n * factorial(n - 1) // Not tail recursive: multiplication after recursive call
// Traditional loop-based implementation
fun factorialLoop(n: Long): Long {
var result = 1L
for (i in 1..n) {
result *= i
}
return result
}
// Compares results of different implementations
val n = 5L
println("Tail recursive factorial: ${factorial(n, 1)}")
println("Non-tail recursive factorial: ${factorial(n)}")
println("Loop-based factorial: ${factorialLoop(n)}")
// All print: 120
//sampleEnd
}(18:50, 18:51) ERROR Too many arguments for 'local final fun /factorial(n: kotlin/Long): kotlin/Long'. |
Contributor
|
Bad samples fun main() {
//sampleStart
// Non-tail-recursive factorial (less efficient, risk of stack overflow)
fun factorial(n: Long): Long =
if (n <= 1) 1
else n * factorial(n - 1) // Not tail recursive: multiplication after recursive call
// Traditional loop-based implementation
fun factorialLoop(n: Long): Long {
var result = 1L
for (i in 1..n) {
result *= i
}
return result
}
// Compares results of different implementations
val n = 5L
println("Tail recursive factorial: ${factorial(n, 1)}")
println("Non-tail recursive factorial: ${factorial(n)}")
println("Loop-based factorial: ${factorialLoop(n)}")
// All print: 120
//sampleEnd
}(18:54, 18:55) ERROR Too many arguments for 'local final fun /factorial(n: kotlin/Long): kotlin/Long'. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.