programdemos/combinators.xtl

Combinators: the Combinators library, Smullyan's birds, each a function that only rearranges, repeats or drops its arguments. Run it with "xetal run demos/combinators.xtl" (or "./demos/combinators.xtl"), or as a notebook with "just show demos/combinators.xtl"; it prints what each bird makes of small numbers, ending with 10 factorial from Y and 1024 from a function power. docs/birds.md lists every bird.

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

The library and two helpers

ᵘi̲nc : Num a => a -> a

function · line 17

u:i_nc x: x plus one.

ᵘi̲nc ← { ⍵ + 1 }

ᵘd̲ouble : Num a => a -> a

function · line 20

u:d_ouble x: x times two.

ᵘd̲ouble ← { ⍵ × 2 }

Recursion from Y, and function power

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

function · line 52

's_elf u:f_act n: the factorial of n, recursing through s_elf, the function it is handed (lazy, ~), rather than by its own name. Y, the Sage Bird, hands it itself: 'ᵘf̲act ᶜY̲ 10 is 10 factorial.

ᵘf̲act ← { ~s̲elf n → n ≤ 1 ? 1◆ n × s̲elf n − 1 }