The advantage of CL is that it provides an interactive environment for computation. The Monte Carlo method, which relies on repeated realizations of a random process, is a useful tool for exploring stochastic processes.
The ability to create macros in lisp will allow us to evaluate an arbitrary piece of code that contains a stochastic element. Rather than editing the source and recompiling every time we want to evaluate a new Monte Carlo model we can just write a new function and evaluate it with the macro.
(defmacro monte-carlo (code-to-run n)
"A macro for taking code which has a random component and running the
same piece of code n times and collecting the results."
(let ((g (gensym)))
`(let ((,g nil))
(do ((i 0 (+ i 1)))
((>= i ,n) ,g)
(setf ,g
(cons
,code-to-run
,g))))))A simple function for computing the mean of a list of values needs to be defined.
(defun mean (nlist) (/ (reduce #'+ nlist) (length nlist)))
CL-USER> (mean (monte-carlo (* (random 1.0) (random 1.0)) 10))
0.28285187
CL-USER> (mean (monte-carlo (* (random 1.0) (random 1.0)) 100))
0.24429739
CL-USER> (mean (monte-carlo (* (random 1.0) (random 1.0)) 1000))
0.24217983
CL-USER> (mean (monte-carlo (* (random 1.0) (random 1.0)) 10000))
CL-USER> (mean (monte-carlo (+ (random 1.0) (random 1.0)) 10))
1.0999103
CL-USER> (mean (monte-carlo (+ (random 1.0) (random 1.0)) 100))
1.043648
CL-USER> (mean (monte-carlo (+ (random 1.0) (random 1.0)) 1000))
1.0064476
CL-USER> (mean (monte-carlo (+ (random 1.0) (random 1.0)) 10000))
0.9966