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.
Division that can fail
ᵘd̲iv : (Num a, Any b) => a -> a -> b -> (Float -> b) -> b
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
x halved, as a maybe.
ᵘh̲alf ← { x → x ᵘd̲iv 2 }
Used in: demos/monads.xtl:31, demos/monads.xtl:37
ᵘf̲ifth : (Num a, Any b) => a -> b -> (Float -> b) -> b
x divided by 5, as a maybe.
ᵘf̲ifth ← { x → x ᵘd̲iv 5 }
Used in: demos/monads.xtl:31
ᵘb̲yZero : (Num a, Any b) => a -> b -> (Float -> b) -> b
x divided by 0: always the empty maybe.
ᵘb̲yZero ← { x → x ᵘd̲iv 0 }
Used in: demos/monads.xtl:37