Lambda Discipline

Examples

A standard library

examples/std.lbd

-- A small standard library written in Lambda Discipline itself.
-- Include it with `use "std.lbd"` (paths are relative to the including file).

-- Combinators
id : a -> a = \x. x
const : a -> b -> a = \x. \_. x
flip : (a -> b -> c) -> b -> a -> c = \f. \x. \y. (f y x)
compose : (b -> c) -> (a -> b) -> a -> c = \f. \g. \x. (f (g x))
pipe : (a -> b) -> (b -> c) -> a -> c = \f. \g. \x. (g (f x))

-- Pairs (`fst` and `snd` are builtins)
swap : (a, b) -> (b, a) = \(x, y). (y, x)
-- `(curry f)` takes the parts of the pair `f` expects one at a time; `uncurry` undoes it.
curry : ((a, b) -> c) -> a -> b -> c = \f. \x. \y. (f (x, y))
uncurry : (a -> b -> c) -> (a, b) -> c = \f. \(x, y). (f x y)

-- Lists
count : (a -> Bool) -> List<a> -> Int = \predicate. \xs. (length (filter predicate xs))
all : (a -> Bool) -> List<a> -> Bool = \predicate. \xs. (foldr \x. \rest. (and (predicate x) rest) True xs)
any : (a -> Bool) -> List<a> -> Bool = \predicate. \xs. (foldr \x. \rest. (or (predicate x) rest) False xs)
product : List<Int> -> Int = (foldl mul 1)
-- The largest element, or `None` for an empty list.
maximum : Ord<a> => List<a> -> Option<a> = \xs. (mapOption \first. (foldl max first xs) (head xs))
minimum : Ord<a> => List<a> -> Option<a> = \xs. (mapOption \first. (foldl min first xs) (head xs))

-- Actions
-- Performs `action` on every element in order: `(forEach println names)`.
forEach : (a -> IO<b>) -> List<a> -> IO<Unit> = \action. \xs. (foldr \x. \rest. (then (action x) rest) (pure ()) xs)

Output

Nothing: this file only defines things for other programs to use.

Run it yourself from the repository root: lbd run examples/std.lbd