Loading [MathJax]/extensions/TeX/AMSsymbols.js
8.11

4.5 (◦ f) ◦ g

2025-03

Josh likes point-free style in Haskell. The other day he asked me, "If f is a unary function and g is a binary function, do you know what (◦ f) ◦ g is?"

Well, it’s not straightforward to me, and I needed to do some mental gymnastics. Fortunately, I have Mathematica.

Composition[
    CurryApplied[Composition, {2, 1}][CurryApplied[f, 1]],
    CurryApplied[g, 2]
][x][y]

The expression above evaluates to g[x, f[y]], and to make sure, I tried:

Composition[
    CurryApplied[Composition, {2, 1}][CurryApplied[Plus, 2][3]],
    CurryApplied[#1^2 + #2^2 &, 2]
][1][2]

That expression expands to 1^2 + (2 + 3)^2 = 26, which verifies my first answer.

Then I deduced it in Haskell:

-- let h = (. f) . g
-- -- then
-- h x = ((. f) . g) x
--     = (. f) (g x)
--     = (g x) . f
-- -- then
-- h x y = ((g x) . f) y
--       = g x (f y)

-- -- a test case:
((. (+3)) . (\x y -> x^2 + y^2)) 1 2 -- yields 26

So (◦ f) ◦ g is equivalent to λxy. g(x, f(y)).

I am starting to love point-free style.