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

Combinators

We already know about higher order functions, functions which take other functions as arguments. Certain higher order functions return other functions; these are sometimes called combinators, especially when the "combine" two or more functions.

    #lang racket
    (require plot)
    (require "calculus.rkt")
(plot
    (function
     (lambda (x) (- (deriv sin x) (cos x))))
    #:x-min -2pi
    #:x-max 2pi)
    (define (fsub f g)
      (lambda (x) (                ))

    (module+ test
      (require rackunit)
      (define (sin2 x) (integrate cos x))
      (define (cos2 x) (deriv sin x))
      (define epsilon 0.001)
      (define test-points (build-list 20 (lambda (x) (* 2 pi (random)))))
      (for ([x test-points])
        (check-= ((fsub cos cos2) x) 0 epsilon)
        (check-= ((fsub sin sin2) x) 0 epsilon)))
    (plot (function (fsub (lambda (x) (deriv sin x)) cos) -2pi 2pi))
    (define (fderiv f)
      (lambda (x) (deriv f x)))

    (plot (function (fsub (fderiv sin) cos) -2pi 2pi))
    (define (binop->fbinop op)
      (lambda (f g)
        (lambda (x)                 )))

    (define fsub2 (binop->fbinop -))

    (plot (function (fsub2 (fderiv sin) cos) -2pi 2pi)))

    (define fadd (binop->fbinop +))
    (plot (function (fadd  sin cos) -2pi 2pi))