Tutorial
Getting Started
Welcome to Ajisai! Try examples in the interactive demo.
Lesson 1: The Stack
Ajisai is Fractional Dataflow. Values are pushed onto a stack:
[ 1 ] # Stack: [ 1 ]
[ 2 ] # Stack: [ 1 ] [ 2 ]
[ 3 ] # Stack: [ 1 ] [ 2 ] [ 3 ]
Operations use and push to the stack:
[ 2 ] [ 3 ] + # Takes both, pushes [ 5 ]
[ 4 ] * # Takes [ 5 ] and [ 4 ], pushes [ 20 ]
Lesson 2: Vectors
# Create vectors
[ 1 2 3 ] # Vector with 3 numbers
[ 'a' 'b' 'c' ] # Vector with strings
[ 1 'hello' TRUE ] # Mixed types OK!
# Operations
[ 1 2 3 4 5 ] LENGTH # Result: [ 5 ]
[ 10 20 30 ] [ 1 ] GET # Result: [ 20 ]
[ 1 2 3 ] REVERSE # Result: [ 3 2 1 ]
# Arithmetic
[ 1 2 3 ] [ 4 5 6 ] + # Result: [ 5 7 9 ]
[ 2 4 6 ] [ 2 ] / # Result: [ 1 2 3 ]
Lesson 3: Defining Words
Use code block syntax { ... } (or ( ... )) to define words:
# Define a word
{ [ 2 ] * } 'DOUBLE' DEF
# Use it
[ 5 ] DOUBLE # Result: [ 10 ]
[ 3 ] DOUBLE # Result: [ 6 ]
# More examples
{ DUP * } 'SQUARE' DEF
[ 4 ] SQUARE # Result: [ 16 ]
{ [ 2 ] MOD [ 0 ] = } 'EVEN?' DEF
[ 4 ] EVEN? # Result: [ TRUE ]
Lesson 4: Higher-Order Functions
# MAP - apply to each
[ 1 2 3 4 5 ] 'DOUBLE' MAP # Result: [ 2 4 6 8 10 ]
# FILTER - keep matching
[ 1 2 3 4 5 6 ] 'EVEN?' FILTER # Result: [ 2 4 6 ]
# FOLD - reduce to one
[ 1 2 3 4 5 ] [ 0 ] '+' FOLD # Result: [ 15 ]
# Chain with pipeline operator (==)
[ 1 2 3 4 5 6 7 8 9 10 ]
== 'EVEN?' FILTER # [ 2 4 6 8 10 ]
== 'SQUARE' MAP # [ 4 16 36 64 100 ]
== [ 0 ] '+' FOLD # [ 220 ]
# Using inline code blocks
[ 1 2 3 4 5 ]
== { [ 2 ] * } MAP # [ 2 4 6 8 10 ]
== { [ 5 ] < NOT } FILTER # [ 6 8 10 ]
Lesson 4.5: Pipeline & Nil Coalescing
== is a visual marker for data flow (no-op):
[ 1 2 3 ]
== { [ 2 ] * } MAP # Clear flow visualization
== [ 0 ] { + } FOLD
=> returns alternative when NIL:
NIL => [ 0 ] # Result: [ 0 ]
[ 42 ] => [ 0 ] # Result: [ 42 ]
# Safe defaults
'key' LOOKUP => [ 'default' ]
Lesson 5: Conservation-Preserving Fraction Arithmetic
# All numbers are fractions
[ 1/3 ] [ 1/6 ] + # Result: [ 1/2 ] (exact!)
[ 0.1 ] [ 0.2 ] + # Result: [ 3/10 ] (no float errors)
# Rounding when needed
[ 7/3 ] FLOOR # Result: [ 2 ]
[ 7/3 ] CEIL # Result: [ 3 ]
Practice Exercises
1. Calculate Average
Define AVERAGE for a vector.
Solution
{ DUP [ 0 ] '+' FOLD SWAP LENGTH / } 'AVERAGE' DEF
[ 2 4 6 8 10 ] AVERAGE # Result: [ 6 ]
2. Factorial
Calculate 6! using RANGE and FOLD.
Solution
[ 1 ] [ 6 ] RANGE [ 1 ] '*' FOLD # Result: [ 720 ]
3. Sum of Multiples
Sum numbers 1-20 divisible by 3.
Solution
{ [ 3 ] MOD [ 0 ] = } 'DIV3?' DEF
[ 1 ] [ 20 ] RANGE
'DIV3?' FILTER
[ 0 ] '+' FOLD # Result: [ 63 ]