macro librarylib/Combinators.xtlm

Combinators as macros: the birds of the Combinators library written in at compile time instead of applied when the program runs. Import both under one alias: "c:" u_se< "Combinators" finds Combinators.xtl (c:Y_, c:B_, ...) and this file (c:Y_<, c:B_<).

source

Recursion

ᵐY̲< : Char -> Char -> Char

macro · line 19

Define the function named on the left by the lambda on the right, whose first parameter is the function itself: the knot c:Y_ ties when the program runs is tied here, by writing the name in place of the self parameter. The result is plain named recursion, with no lazy parameter and no combinator left to run.

      "c:" u̲se< "Combinators"
      "u:f_act" ᶜY̲< "{ ~s_elf n -> n <= 1 ? 1; n * s_elf n - 1 }"
      ᵘf̲act 5
120
ᵐY̲< ← { name lambda →
  n̲ot ⎕S̲TATEMENT @ ? "misplaced-macro call" ⎕R̲EJECT "c:Y_< defines a function: it stands as a statement of its own"
  t ← ʰt̲rim lambda
  a ← w̲here "->" ʰs̲tarts t
  (n̲ot "{" m̲atch 1 t̲ake t) ∨ (0 = t̲ally a) ? "bad-macro-argument right" ⎕R̲EJECT yShape
  ps ← ʰw̲ords ((f̲irst a) − 2) t̲ake 1 d̲rop t
  2 > t̲ally ps ? "bad-macro-argument right" ⎕R̲EJECT yShape
  self ← ʰs̲trip d̲isclose f̲irst ps
  rest ← ʰj̲oin '{ p → " " c̲at d̲isclose p } m̲ap 1 d̲rop ps
  body ← ((f̲irst a) + 1) d̲rop t
  (ʰt̲rim name) c̲at " := {" c̲at rest c̲at " ->" c̲at (self ʰw̲ord ʰt̲rim name) body
}

Composition

ᵐB̲< : Char -> Char -> Char

macro · line 42

The function on the left applied to the result of the one on the right, as one lambda written in place (the Bluebird, B f g x = f (g x)); either may be a name, a derived function or a lambda.

      "c:" u̲se< "Combinators"
      ᵘs̲umTo ← "'+ r_/" ᶜB̲< "r_ange"
      ᵘs̲umTo 4
10
ᵐB̲< ← { f g → "{ " c̲at (ʰt̲rim f) c̲at " " c̲at (ʰt̲rim g) c̲at " _r }" }

yShape : Char

value (private) · line 44
yShape ← "c:Y_< takes a lambda whose first parameter is the function itself: { s_elf n -> ... }"
Used in: ᵐY̲<

ʰt̲rim : Char -> Char

function (private) · line 47
ʰt̲rim ← { t →
  m ← n̲ot t m̲ember? " \n\t"
  0 = '+ r̲/ 0 + m ? ""
  i ← w̲here m
  (1 + (f̲irst r̲ev i) − f̲irst i) t̲ake ((f̲irst i) − 1) d̲rop t
}
Used in: ᵐY̲<, ᵐB̲<

ʰw̲ords : Char -> Box Char

function (private) · line 55
ʰw̲ords ← { t → (n̲ot t m̲ember? " \n\t") p̲artition t }
Used in: ᵐY̲<

ʰj̲oin : Box Char -> Char

function (private) · line 58
ʰj̲oin ← { b → d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } r̲/ b }
Used in: ᵐY̲<, ʰw̲ord

ʰs̲trip : Char -> Char

function (private) · line 61
ʰs̲trip ← { p → (f̲irst p) = f̲irst "~" ? 1 d̲rop p◆ p }
Used in: ᵐY̲<

ʰs̲tarts : Char -> Char -> Int

function (private) · line 64
ʰs̲tarts ← { w t → '{ i → w m̲atch (t̲ally w) t̲ake (i − 1) d̲rop t } e̲ach r̲ange t̲ally t }
Used in: ᵐY̲<, ʰw̲ord

nameChars : Char

value (private) · line 67
nameChars ← "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_:"
Used in: ʰw̲ord

ʰw̲ord : Char -> Char -> Char -> Char

function (private) · line 70
ʰw̲ord ← { w r t →
  n ← t̲ally w
  e ← " " c̲at t c̲at " "
  ok̲ ← { i → (i + n − 1) > t̲ally t ? 0◆ n̲ot ((i s̲elect e) m̲ember? nameChars) ∨ (i + n + 1) s̲elect e m̲ember? nameChars }
  h ← (w ʰs̲tarts t) ∧ 'ok̲ e̲ach r̲ange t̲ally t
  c ← 0 < ('+ s̲\ 0 + h) − '+ s̲\ 0 + (n r̲eshape 0) c̲at (0 − n) d̲rop 0 + h
  p̲ ← { i → 0 < i s̲elect h ? r◆ (i s̲elect c) ? ""◆ i s̲elect t }
  ʰj̲oin 'p̲ m̲ap r̲ange t̲ally t
}
Used in: ᵐY̲<