Skip to content

Conversation

zamulla
Copy link
Contributor

@zamulla zamulla commented Feb 17, 2025

No description provided.

@AlexanderPrendota
Copy link
Collaborator

Bad samples
Snippets:
File: docs/topics/functions.md

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'.


* correct indentations in functions.md
* add (not always fitting, but correct) print comments to generics.md, idioms.md, and interfaces.md
@AlexanderPrendota
Copy link
Collaborator

Bad samples
Snippets:
File: docs/topics/functions.md

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'.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants