Skip to main content

Overview

The FizzBuzz example is a classic programming challenge that demonstrates conditional logic, loops, and function definitions in Dryft.

Source Code

include: std/io 

(Int Int -> Bool) 
fun: divby
	mod 0 =? ;

(Int ->)
act: fizzbuzz var: max
	1 var: x
	cycle: $x $max >? then: break ;
		when: 
			$x 15 divby then: "FizzBuzz!" prints ;
			$x  3 divby then: "Fizz" prints ;
			$x  5 divby then: "Buzz" prints ;
			$x printi ;
		"\n" prints
		$x 1 + x! 
	:cycle
:act

act: main 100 fizzbuzz ;

What This Example Demonstrates

This example showcases:
  • Function signatures: Type annotations like (Int Int -> Bool) define input and output types
  • Custom functions: The divby function 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

(Int Int -> Bool) 
fun: divby
	mod 0 =? ;
Takes two integers, computes the modulo, and checks if the result equals zero (indicating divisibility).

The fizzbuzz Action

  1. Takes a max parameter defining the upper limit
  2. Initializes a counter x starting at 1
  3. Loops until x exceeds max:
    • If divisible by 15: prints “FizzBuzz!”
    • Else if divisible by 3: prints “Fizz”
    • Else if divisible by 5: prints “Buzz”
    • Otherwise: prints the number
  4. 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.

Compiling and Running

To compile and run this example:
1

Compile the program

dryft compile examples/fizzbuzz.dry -o fizzbuzz
2

Run the executable

./fizzbuzz

Expected Output

The program will output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz!
...
This example is excellent for understanding how Dryft handles control flow and how the stack-based paradigm applies to conditional logic.

Build docs developers (and LLMs) love