====== Lambda Calculus ======
==== Point-Free Style: compose-compose pattern ====
''(◦ f) ◦ g '' is equivalent to ''λxy. g(x, f(y))''.
In &HS 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)
((. (+3)) . (\x y -> x^2 + y^2)) 1 2 -- yields 26
In &MMA Mathematica:
Composition[
CurryApplied[Composition, {2, 1}][CurryApplied[f, 1]],
CurryApplied[g, 2]
][x][y] (* yields g[x, f[y]] *)
Composition[
CurryApplied[Composition, {2, 1}][CurryApplied[Plus, 2][3]],
CurryApplied[#1^2 + #2^2 &, 2]
][1][2] (* yields 26 *)
In &RKT Racket:
#lang racket
(define (comp-comp f g)
(let [[cf (curry f)]
[cg (curry g)]]
(compose (lambda (x) (compose x cf)) cg)))
(define (f x) (+ x 3))
(define (g x y) (+ (* x x) (* y y)))
(((comp-comp f g) 1) 2) ;; yields 1^2 + (2+3)^2 = 26
There's a package called ''relation-lib'', the function ''uncurry'' in it makes things simpler:
#lang racket
(require relation/function)
(define (comp-comp f g)
(let [[cf (curry f)]
[cg (curry g)]]
(uncurry (compose (lambda (x) (compose x cf)) cg))))
(define (f x) (+ x 3))
(define (g x y) (+ (* x x) (* y y)))
((comp-comp f g) 1 2) ;; yields 1^2 + (2+3)^2 = 26