programdemos/monads.xtl

The Maybe monad from the Maybe library: computations that can fail, chained so that the first failure stops the chain.

Run it with ./demos/monads.xtl or "xetal run demos/monads.xtl", or as a notebook with "just show demos/monads.xtl". Each line prints a result, or the default (-1, or 0 for m_aybe) where a division by zero emptied the maybe. Read how b_ind chains steps right to left.

source · imports m: lib/Maybe.xtl

Division that can fail

ᵘd̲iv : (Num a, Any b) => a -> a -> b -> (Float -> b) -> b

function · line 16

a u:d_iv b: a / b in a maybe, or the empty maybe when b is 0, instead of stopping the program.

ᵘd̲iv ← { a b → b = 0 ? 'ᵐn̲othing◆ ᵐj̲ust a ÷ b }

Chains

ᵘh̲alf : (Num a, Any b) => a -> b -> (Float -> b) -> b

function · line 28

x halved, as a maybe.

ᵘh̲alf ← { x → x ᵘd̲iv 2 }

ᵘf̲ifth : (Num a, Any b) => a -> b -> (Float -> b) -> b

function · line 30

x divided by 5, as a maybe.

ᵘf̲ifth ← { x → x ᵘd̲iv 5 }

ᵘb̲yZero : (Num a, Any b) => a -> b -> (Float -> b) -> b

function · line 36

x divided by 0: always the empty maybe.

ᵘb̲yZero ← { x → x ᵘd̲iv 0 }