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.
Numbers
x : Int
A small Int, squared and reused below.
x ← 3 ⍝ ← binds; = is always equality
Mutation
count! : Int
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
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
u:s_quare x: x times x, item by item.
ᵘs̲quare ← { ⍵ × ⍵ } ⍝ ⍵ is the right argument
ᵘs̲ub : Num a => a -> a -> a
a u:s_ub b: a minus b.
ᵘs̲ub ← { ⍺ − ⍵ } ⍝ ⍺ is the left argument
ᵘh̲yp : Float -> Float -> Float
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
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
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
u:t_wo @: 2; it takes only Unit.
ᵘt̲wo ← { @ → 2 } ⍝ a niladic function takes @
ᵘk̲eep : (Any a, Any b) => a -> b -> a
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
'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
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
Search and order
Nested arrays and display
n : Box Char
A vector of two boxed strings.
n ← "ab" "cde" ⍝ a strand of strings: a vector of 2 boxes
Combinators and power
ᵘt̲riangle : Num a => (a -> a) -> a -> a
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
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 } ⍵ }