tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

fold.scp (566B)


      1 !export fold
      2 -- Left fold over a list. Input: elements in order; state: (worker, accumulator).
      3 -- The worker receives (current accumulator, element), with its own initial
      4 -- state, and returns the next accumulator. On [] return the accumulator.
      5 -- Example: with prepend as worker and [] as initial accumulator, [1 2 3]
      6 -- folds to [3 2 1]. Worker failure propagates; this is not an effect handler.
      7 consumer fold
      8   []       @ (worker, accumulator) => accumulator
      9   [x | xs] @ (worker, accumulator) => call worker (accumulator, x) as next in recur xs @ (worker, next)