# [racket] Function composition in Racket

Instead of trying to peek inside a lambda, you could implement polynomials as (transparent) structs and use prop:procedure to allow them to be applied to numbers:
(struct polynomial (coeffs)
#:transparent
#:property prop:procedure
(lambda (poly num)
(define-values (result x*)
(for/fold ([sum 0]
[x 1])
([c (polynomial-coeffs poly)])
(values (+ sum (* c x))
(* x num))))
result))
This would let you implement functions that crunch on polynomials. As for using existing operators' names, maybe generics can get you that?
---
Justin Slepak
PhD student, Computer Science dept.
----- Original Message -----
From: Gregory Woodhouse <gregwoodhouse at me.com>
To: Racket Mailing List <users at racket-lang.org>
Sent: Sun, 14 Oct 2012 19:00:19 -0400 (EDT)
Subject: [racket] Function composition in Racket
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)
(cond
[(= (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)
or
(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?
____________________
Racket Users list:
http://lists.racket-lang.org/users