UNB/ CS/ David Bremner/ teaching/ cs4613/ tutorials/ curry

# Currying

• We can take the definition of fderiv and abstract out the definition of deriv; let's call it `g`.
```    (define (mogrify g f)
(lambda (x) (g f x)))

(define (fderiv2 f) (mogrify deriv f))

(plot (function (fsub (fderiv2 sin) cos) -2pi 2pi))
```
• In the spirit of the discussion above, the argument `f` to `mogrify` is kind of useless. We can hide it inside a new combinator:
```    (define (currify g)
(lambda (f) (lambda (x) (g f x))))

(define fderiv3 (currify deriv))
(module+ test
(for ([x test-points])
(check-= (cos x) ((fderiv3 sin) x) epsilon)))
```
• The combinator `currify` is common enough to be built into the racket library under the name curry

• You can verify that `curry` is a drop replacement for the function `currify`; if you read the documentation you can see that it's actually more general.

• for whatever reason `curry` is not built into plait, but our definition if `currify` should work fine there.