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>
This commit is contained in:
25
examples/standard/guessing_game.lux
Normal file
25
examples/standard/guessing_game.lux
Normal file
@@ -0,0 +1,25 @@
|
||||
// Number guessing game - demonstrates Random and Console effects
|
||||
|
||||
fn gameLoop(secret: Int, attempts: Int): Unit with {Console} = {
|
||||
Console.print("Guess the number (1-100), attempt " + toString(attempts) + ":")
|
||||
let guess = Console.readInt()
|
||||
if guess == secret then
|
||||
Console.print("Correct! You got it in " + toString(attempts) + " attempts!")
|
||||
else if guess < secret then {
|
||||
Console.print("Too low!")
|
||||
gameLoop(secret, attempts + 1)
|
||||
}
|
||||
else {
|
||||
Console.print("Too high!")
|
||||
gameLoop(secret, attempts + 1)
|
||||
}
|
||||
}
|
||||
|
||||
fn main(): Unit with {Console, Random} = {
|
||||
Console.print("Welcome to the Guessing Game!")
|
||||
Console.print("I'm thinking of a number between 1 and 100...")
|
||||
let secret = Random.int(1, 100)
|
||||
gameLoop(secret, 1)
|
||||
}
|
||||
|
||||
let output = run main() with {}
|
||||
Reference in New Issue
Block a user