programdemos/tour.xtl

A tour of X_eTaL: one commented file touching every implemented feature, a section per topic, from numbers to Life in one line.

Run it with ./demos/tour.xtl or "xetal run demos/tour.xtl": each top-level expression prints its value (the dice rolls differ on every run). just tour shows each line decorated with its output; just pp demos/tour.xtl prints it decorated; just edit demos/tour.xtl opens the ASCII and the decorated form side by side.

Source is plain ASCII; the display draws the decorations (code in backquotes inside comments is drawn too: x² r̲ev ⍵ ←). The table of what to type for each glyph is docs/input.md. Expressions read right to left; read each line with its trailing comment.

source · imports s: lib/Stats.xtl; hi: userlibs/Hello.xtl; c: lib/Combinators.xtl lib/Combinators.xtlm

Numbers

x : Int

value · line 20

A small Int, squared and reused below.

x ← 3                    ⍝ ← binds; = is always equality

Mutation

count! : Int

value · line 35

A counter: the one kind of name that may be bound again.

count! ← 0               ⍝ only names ending in ! (like count!) may be reassigned

Arrays

m : Int

value · line 47

A 2 by 3 matrix of 1 to 6, used by the sections below.

m ← 2 3 r̲eshape r̲ange 6          ⍝ 1-origin: r̲ange 6 is 1 2 3 4 5 6

Functions

ᵘs̲quare : Num a => a -> a

function · line 63

u:s_quare x: x times x, item by item.

ᵘs̲quare ← { ⍵ × ⍵ }              ⍝ ⍵ is the right argument

ᵘs̲ub : Num a => a -> a -> a

function · line 66

a u:s_ub b: a minus b.

ᵘs̲ub ← { ⍺ − ⍵ }                 ⍝ ⍺ is the left argument

ᵘh̲yp : Float -> Float -> Float

function · line 69

a u:h_yp b: the hypotenuse of a right triangle with legs a and b.

ᵘh̲yp ← { a b → ((a × a) + b × b) ^ 0.5 }       ⍝ named parameters before →

ᵘs̲ign : (Num a, Num b) => a -> b

function · line 72

u:s_ign x: -1, 0 or 1 as x is negative, zero or positive.

ᵘs̲ign ← { x → x < 0 ? -1◆ x = 0 ? 0◆ 1 }      ⍝ guards: condition ? result

ᵘf̲act : Num a => a -> a

function · line 74

u:f_act n: the factorial of n, by recursion (1 for n <= 1).

ᵘf̲act ← { n →
  n ≤ 1 ? 1
  n × ᵘf̲act n − 1
}

ᵘt̲wo : Num a => Unit -> a

function · line 80

u:t_wo @: 2; it takes only Unit.

ᵘt̲wo ← { @ → 2 }                 ⍝ a niladic function takes @

ᵘk̲eep : (Any a, Any b) => a -> b -> a

function · line 83

a u:k_eep b: a; b is lazy, so it is never evaluated.

ᵘk̲eep ← { a ~b → a }             ⍝ ~b is a lazy parameter: evaluated

ᵘt̲wice : Any a => (a -> a) -> a -> a

function · line 87

'f u:t_wice x: f applied to x two times.

ᵘt̲wice ← { f̲ x → f̲ f̲ x }         ⍝ apply a function parameter two times

Trains and tacks

ᵘa̲vg : Int -> Float

function · line 106

u:a_vg x: the mean of x, as a fork of sum, divide and tally.

ᵘa̲vg ← ['+ r̲/ ÷ t̲ally]           ⍝ fork: ('+ r̲/ x) ÷ t̲ally x

Rotate, reverse and axes

a : Int

value · line 135

A 2 by 3 by 4 array of 1 to 24, for the axis examples.

a ← 2 3 4 r̲eshape r̲ange 24

Search and order

v : Int

value · line 142

The vector the search and order examples use.

v ← 3 1 4 1 5 9 2 6

Nested arrays and display

n : Box Char

value · line 151

A vector of two boxed strings.

n ← "ab" "cde"           ⍝ a strand of strings: a vector of 2 boxes

s : Char

value · line 156

A sentence to cut into words.

s ← "to be or not"

Combinators and power

ᵘt̲riangle : Num a => (a -> a) -> a -> a

function · line 190

u:t_riangle n: 1 + 2 + ... + n, with s_elf standing for the function itself (lazy, so u:t_riangle is given to c:Y_ to recurse).

ᵘt̲riangle ← { ~s̲elf n → n ≤ 1 ? 1◆ n + s̲elf n − 1 }

Life in one line

ᵘl̲ife : (Num a, Truthy a) => a -> a

function · line 198

u:l_ife b: the next generation of the Game of Life on board b (0 dead, 1 alive), the board wrapping at its edges: a cell lives when its 3 by 3 neighborhood total is 3, or 4 and it is alive.

ᵘl̲ife ← { ('+ r̲/₁₂ -1 0 1 o̲-₁₂ ⍵) { (⍺ = 3) + ⍵ × ⍺ = 4 } ⍵ }