programdemos/keys.xtl

Keys: every way X_eTaL is typed. Each line of code appears twice: drawn decorated, as "just pp demos/keys.xtl" shows it, and at the end of the line, after "typed:", exactly the keys you type (a comment is drawn as written, so that text stays plain ASCII). Run it with "xetal run demos/keys.xtl" (or "./demos/keys.xtl") to print each line's result, or "just show demos/keys.xtl" to see each line next to its result. docs/input.md explains each form.

source · imports c: lib/Combinators.xtl lib/Combinators.xtlm

Names and bindings

x : Int

value · line 13

x: a plain variable. := binds it; it is drawn as a left arrow.

x ← 5                                   ⍝ typed: x := 5

count! : Int

value · line 17

count!: a variable whose name ends in ! may be bound again (mutation); the ! is part of the name wherever it is used.

count! ← 0                              ⍝ typed: count! := 0

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

function · line 27

u:s_quare x: x times x. u: puts a name in your program's own namespace (drawn as a raised u). Inside { }, _r is the right argument (drawn as APL's omega).

ᵘs̲quare ← { ⍵ × ⍵ }                     ⍝ typed: u:s_quare := { _r * _r }

Reduce, scan, rotate and reverse, and their axes

M : Int

value · line 34

M: a matrix of 2 rows and 3 columns, 1 2 3 above 4 5 6. Axis 1 runs down the rows (its length is 2), axis 2 along a row (length 3).

M ← 2 3 r̲eshape r̲ange 6                 ⍝ typed: M := 2 3 r_eshape r_ange 6

Functions of your own

ᵘa̲dd : Num a => a -> a -> a

function · line 141

x u:a_dd y: x plus y. Inside { }, _l is the left argument (drawn as APL's alpha) and _r the right: this function takes two arguments.

ᵘa̲dd ← { ⍺ + ⍵ }                        ⍝ typed: u:a_dd := { _l + _r }

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

function · line 149

u:s_ign n: -1, 0 or 1 as n is negative, zero or positive. n -> names the argument n. "c ? r" is a guard: when c holds, the result is r; statements are separated by ; (drawn as a diamond).

ᵘs̲ign ← { n → n < 0 ? -1◆ n = 0 ? 0◆ 1 }

ᵘm̲ean : Int -> Float

function · line 159

u:m_ean v: the mean of v. Three functions in square brackets are a train (a fork): the sum divided by the count, which is the mean.

ᵘm̲ean ← ['+ r̲/ ÷ t̲ally]                 ⍝ typed: u:m_ean := ['+ r_/ / t_ally]