Integral Calculus in Lambda Calculus (Lisp)

A Lisp version of the code from this article –>

http://iam.elbenshira.com/archives/151_integral-calculus-in-haskell/

**********
For those that are unhappy about this not being a Symbolic Integration example then please read Norvig’s “Paradigms of Artificial Intelligence Programming – Case Studies in Common Lisp”, Chapter 8 – Symbolic Mathematics : A Simplification Program, 8.6 – Integration, page 252.

http://norvig.com/paip.html

**********


(defparameter *small-dx* 0.0001)

(defun integrate(f a b)
  "integrate a function f from a to b"

    (defun integrate-gen(f x b dx sum)
      "the generalized inner function for tail-recursion.
       integrate a function f from x to b"
      (if (> x b)
          sum
          (integrate-gen f (+ x dx) b dx (+ sum (* (funcall f x) dx)))))

    (integrate-gen f a b *small-dx* 0))

(let 
    ((f1 (lambda (x) 1))
     (f2 (lambda (x) (expt x 2)))
     (f3 (lambda (x) (* x (exp (expt x 2))))))
  (print (integrate f1 0 5))
  (print (integrate f3 0 1)))

Leave a Reply

Your email address will not be published. Required fields are marked *