Overview
The FizzBuzz example is a classic programming challenge that demonstrates conditional logic, loops, and function definitions in Dryft.Source Code
What This Example Demonstrates
This example showcases:- Function signatures: Type annotations like
(Int Int -> Bool)define input and output types - Custom functions: The
divbyfunction checks if a number is divisible by another - Variable declaration: Using
var:to declare and bind variables - Variable reference: Using
$prefix to reference variable values - Loops: The
cycle:construct for iteration - Conditional logic: Using
when:for multi-branch conditionals - Break statements: Exiting loops with
break - Variable mutation: Using
!suffix to update variable values
How It Works
The divby Function
The fizzbuzz Action
- Takes a
maxparameter defining the upper limit - Initializes a counter
xstarting at 1 - Loops until
xexceedsmax:- If divisible by 15: prints “FizzBuzz!”
- Else if divisible by 3: prints “Fizz”
- Else if divisible by 5: prints “Buzz”
- Otherwise: prints the number
- Prints a newline and increments
x
The main Action
Calls fizzbuzz with 100 as the argument to run the FizzBuzz sequence from 1 to 100.
The
when: construct in Dryft evaluates conditions in order and executes the first matching branch.