Skip to main content

Overview

This example demonstrates how to implement common mathematical functions in Dryft, including factorial calculation, exponentiation, and safe division with error handling.

Source Code

include: std/io

# n! #
fun: factorial var: n
	1 var: result
	cycle: $n 0 =? then break ;
		$result $n * result!
		$n 1 - n!
	:cycle
	$result
:fun

# x ^ y #
fun: expo 
	var: x var: y
	1 var: total
	cycle: $x 0 =? then: break ;
		$total $y * total!
		$x 1 - x! 
	:cycle
	$total
:fun

act: safediv 
	var: x var: y
	when:
		$x 0 equals? then: "Division by zero" prints ; 
		$y 0 equals? then: 0 ;
		$x 1 equals? then: $y ;
		$y $x / ;
:act

act: main
	10 factorial printi "\n" prints
	5 3 expo printi "\n" prints
	6 1 safediv printi "\n" prints
	4 0 safediv "\n" prints
:act

What This Example Demonstrates

This example showcases:
  • Comments: Using # for inline comments
  • Iterative algorithms: Implementing mathematical operations using loops
  • Return values: Functions returning computed values by leaving them on the stack
  • Multiple variable bindings: Declaring multiple variables in sequence
  • Arithmetic operations: Using built-in operators like *, -, and /
  • Error handling: Implementing safe operations with validation
  • Function composition: Calling functions from the main action

How It Works

Factorial Function

fun: factorial var: n
	1 var: result
	cycle: $n 0 =? then break ;
		$result $n * result!
		$n 1 - n!
	:cycle
	$result
:fun
Calculates n! iteratively:
  1. Initializes result to 1
  2. Loops while n is not zero
  3. Multiplies result by n and decrements n
  4. Returns the final result
The function leaves the result on the stack, which is how Dryft functions return values.

Exponentiation Function

fun: expo 
	var: x var: y
	1 var: total
	cycle: $x 0 =? then: break ;
		$total $y * total!
		$x 1 - x! 
	:cycle
	$total
:fun
Calculates y^x through repeated multiplication:
  1. Takes two parameters: x (exponent) and y (base)
  2. Initializes total to 1
  3. Multiplies total by y exactly x times
  4. Returns the computed power

Safe Division Action

act: safediv 
	var: x var: y
	when:
		$x 0 equals? then: "Division by zero" prints ; 
		$y 0 equals? then: 0 ;
		$x 1 equals? then: $y ;
		$y $x / ;
:act
Performs division with safety checks:
  1. If divisor is 0: prints error message
  2. If dividend is 0: returns 0
  3. If divisor is 1: returns dividend unchanged
  4. Otherwise: performs the division
Note that the parameters appear reversed: safediv takes divisor first, then dividend, due to stack ordering.

Main Action

Demonstrates all three functions:
  • Calculates 10! (factorial of 10)
  • Calculates 3^5 (3 to the power of 5)
  • Performs safe division: 1/6 and 0/4

Compiling and Running

To compile and run this example:
1

Compile the program

dryft compile examples/math.dry -o math
2

Run the executable

./math

Expected Output

3628800
243
6
Division by zero
Breakdown:
  • 3628800 is 10!
  • 243 is 3^5
  • 6 is 6/1
  • Division by zero message for 4/0
These examples demonstrate how iterative algorithms work in a stack-based language. Notice how variables are mutated using the ! suffix operator.

Build docs developers (and LLMs) love