Written on 2018-01-30
In Challenge #15, we wrote a few simple combinators. Here we write a few more.
In addition to combinator practice, this exercise will be written so as to practice math notation comprehension.
For each of these, implement the combinator and write the most general type for that combinator.
Combinator $C_1$ which "flips" the arguments of binary function. It takes $f : X\times Y\to Z$ to $C_1 f = f' : Y \times X \to Z$ such that for all $x\in X$ and $y \in Y$, $$f(x,y) = f'(y,x).$$ This combinator is an involution because $C_1^2$ is the identity.
Combinator $C_2$ which duplicates an argument to a binary function. It takes $f : X\times X\to Z$ to $C_2 g = f' : X \to Z$ such that for all $x\in X$, $$f(x,x) = f(x).$$
Combinator $C_3$ which takes two functions with the same domain $X$, and produces a function taking $x\in X$ and returning $(f(x), g(x))$.
Combinator $C_4$ which takes a function, and produces a curried function of two arguments, such that the second argument supplied is effectively ignored.
Fun Fact: In Haskell, the combinators above are called flip
, join
,
liftM2 (,)
, and (const .)
respectively.