[racket] Function composition in Racket

From: Gregory Woodhouse (gregwoodhouse at me.com)
Date: Sun Oct 14 19:00:19 EDT 2012

I wrote a small recursive function to convert a list (a0 a1 ... an) coefficients into a polynomial  function 

;;Given a list (a0 a1 ... an) return a function that computes
;;p(x) = a0 + a1*x + ... + an*x^n
(define (polynomial coeffs)
  (lambda (x)
      [(= (length coeffs) 0) 0]
      [(= (length coeffs ) 1) (first coeffs)]
      [else (+ (first coeffs) (* x ((polynomial (rest coeffs)) x)))])))

and it seems to work fairly well. The problem is that it only works for numeric functions. If I define an operator (i.e., a function, usually linear,
that maps functions to functions) such as the difference operator

(delta f)(x) = f(x) - f(x - 1)


(define (delta f)
  (lambda (x)
    (- (f x) (f (- x 1)))))

and p is polynomial, I ought to be able to compute a new operator

p(delta) = a0 + a1*delta + a2*delta^2 + ... + an*delta^n

(where delta^k is just delta composed with itself or, if you prefer, applied k times).

Now, my question is: is there a notation in Racket for representing composition that I should use, or am I better off writing a function that maps an operator
A to p(A) where p is a polynomial?

Posted on the users mailing list.