common lisp let binding
I have a function calculate binomial expansion with optional parameters to specify the beginning and ending term:
(defun comb-index (s k)
(let ((combinations nil))
(labels ((rec (s k offset entry)
(cond ((equal s k)
(push (reverse (loop
for i from 1 to s
do (push (1- (+开发者_如何学编程 i offset)) entry)
finally (return entry)))
combinations))
((equal k 0)
(push (reverse entry) combinations))
(t (rec (1- s) (1- k) (1+ offset) (cons offset entry))
(rec (1- s) k (1+ offset) entry)))))
(rec s k 0 nil))
(nreverse combinations)))
(defun binomial (k &key (start 1) end)
(let ((b start)
(e (if (null end) k end)))
(labels ((rec (i)
(cond ((equal i e)
(comb-index k e))
(t
(append (comb-index k i) (rec (1+ i)))))))
(rec b))
)
)
When I compile and run this code, it will yield the following run time error:
Unhandled memory fault at #x18.
[Condition of type SB-SYS:MEMORY-FAULT-ERROR]
This is caused by e, but I'm not sure why. I can avoid this problem by assigning 'e' with either 'k' or 'end', or simply using a (when ... ) to set 'end' to 'k' if it's nil, but I'm not sure why this doesn't work.
Looks like a memory overflow...
Ever thought about the memory efficiency of your code?
The problem in the code isn't obvious; it just seems to be a real overflow. Is there any way you can adjust the memory available to the underlying Lisp system? Command line flags or something?
Do you have the option of implementing this in a language like Clojure that can create lazy sequences? As you are probably aware, this type of calculation has the potential to create extremely large results. I seem to recall something in one of the Clojure/contrib libraries that did just this calculation.
精彩评论