Files
lux/examples/standard/factorial.lux
Brandon Lucas 0b5abece5f feat: add comprehensive example programs
Standard examples (examples/standard/):
- hello_world: Basic effect usage
- fizzbuzz: Classic programming exercise
- factorial: Recursive and tail-recursive versions
- primes: Prime number generation
- guessing_game: Interactive Random + Console effects
- stdlib_demo: Demonstrates List, String, Option, Math modules

Showcase examples (examples/showcase/):
- ask_pattern: Resumable effects for config/environment
- custom_logging: Custom effect with handler
- early_return: Fail effect for clean error handling
- effect_composition: Combining multiple effects
- higher_order: Closures and function composition
- pattern_matching: ADTs and exhaustive matching

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 17:25:04 -05:00

23 lines
593 B
Plaintext

// Factorial - compute n!
// Recursive version
fn factorial(n: Int): Int =
if n <= 1 then 1
else n * factorial(n - 1)
// Tail-recursive version (optimized)
fn factorialTail(n: Int, acc: Int): Int =
if n <= 1 then acc
else factorialTail(n - 1, n * acc)
fn factorial2(n: Int): Int = factorialTail(n, 1)
fn main(): Unit with {Console} = {
Console.print("Factorial examples:")
Console.print("5! = " + toString(factorial(5)))
Console.print("10! = " + toString(factorial(10)))
Console.print("20! = " + toString(factorial2(20)))
}
let output = run main() with {}