
Integral Calculus in Lambda Calculus (Lisp) - jgrant27
http://jng.imagine27.com/articles/2009-04-09-161839_integral_calculus_in_lambda_calculus_lisp.html
======
jrockway
One other thing. The Lisp is rather non-idiomatic. Here is how I'd write it:

    
    
        (defparameter small-dx 0.00001)
    
        (defun integrate (f a b &optional (dx small-dx))
          "integrate a function f from a to b"
          (loop for x from a to b by dx sum (* dx (funcall f x))))
    

Then you can use it like:

    
    
        (flet ((f1 (x) 1)
               (f2 (x) (expt x 2))
               (f3 (x) (* x (exp (expt x 2)))))
          (format t "f1: ~A, f2: ~A, f3: ~A~%"
                  (integrate #'f1 0 5)
                  (integrate #'f2 0 5)
                  (integrate #'f3 0 5)))
    

In conclusion, CL is not Scheme.

(Edit: Initially I said that the code doesn't compile. It does -- storing
functions in the value slot of variables threw me off a bit. Very Scheme-
like.)

~~~
jgrant27
The code compiles and runs just fine on any CL compiler. Try SBCL or CLozure
CL.

Actually the loop macro in CL can be a very contentious topic. I'd hardly
refer to the loop macro as idiomatic. Imperative and functional styles in CL
can both be idiomatic.

Anyway who said CL was Scheme ?

~~~
jrockway
_Anyway who said CL was Scheme ?_

Your style strongly hints at a Scheme background. Nothing you do is wrong, but
if you read a bunch of Lisp code, you wouldn't see many people doing things
the way you did. OTOH, if you read a bunch of Scheme code, it would look
exactly like yours.

Also, I don't think the use of loop is a point of contention. LOOP is in the
CL spec, tail recursion isn't. (The ITERATE package is a nicer loop, but the
idea is the same.)

~~~
jgrant27
Interesting opinion regarding my style. Thanks for explaining. There are many
things in the CL spec that are very contentious. Arguably too many. Dan
Weinreb pointed this out earlier in the
week(<http://ilc2009.scheming.org/node/7>) which inspired a response from
me([http://jng.imagine27.com/articles/2009-04-07-135246_cl_hard_...](http://jng.imagine27.com/articles/2009-04-07-135246_cl_hard_to_compile_efficiently.html))
although not directly related to the "too many concepts, irregular" argument
but then again this may effect how per formant/stable code can be written in a
language.

Yes tail-recursion is not part of the spec along with many other
implementation aspects that probably should be.

~~~
mahmud
Good on you taking criticism so well jgrant27, testament to a mature mind.

Also, absence of tail-call elimination in the spec doesn't have to stop you
from using it, if your compiler supports it. We all love Common Lisp, but
usually not "warts and all"; we all end up using our personally "attractive"
subset. I personally code in Dylan with s-exp notation that happens to compile
under SBCL :-)

------
rkowalick
I would really impressed by seeing some kind of wacky function paired with a
fixed point combinator so that when a function is integrable, the combinator
spits out as a fixed point the answer.

------
jobeirne
There's a great bit about this in the first chapter of the SICP.

------
jrockway
A fancy title for what amounts to addition and function evaluation.

~~~
jgrant27
What else does integration primarily entail ?

~~~
jrockway
Symbolic integration would be much more interesting than Riemann sums.

~~~
jgrant27
I'm not going to implement that for you. Sorry.

See 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>

