Open arademaker opened 6 years ago
I don't know what compiler or settings you are using, but:
Maybe you should use tracing and/or some other debugging method to figure out what's going on.
Sorry, SBCL 1.4.6! The function K
is not tail recursive.
OK, trying to figure out the problem. It looks like the heap error is the lack of memory before a garbage collector can free space. Does it make sense? The memorization seems to work:
First, I make my code have explicit control of the memo TABLE with:
(defun knapsack (filename &key (memo (make-hash-table :test #'equal)))
(multiple-value-bind (meta items)
(read-file filename)
(let ((*items* items))
(memoize 'K :table memo)
(values (K (car meta) (cadr meta))
memo))))
As expected, the first execution takes some time:
* (multiple-value-list (knapsack "/Users/ar/Sites/ED-2018-1/lista-7/knapsack_big.txt"))
(4243395 #<HASH-TABLE :TEST EQUAL :COUNT 10629293 {1008CD4383}>)
The next one instantly returns:
* (multiple-value-list (knapsack "/Users/ar/Sites/ED-2018-1/lista-7/knapsack_big.txt" :memo (cadr *)))
(4243395 #<HASH-TABLE :TEST EQUAL :COUNT 10629293 {1008CD4383}>)
What's K
?
I didn't really understand the documentation:
What is the special care for recursive functions? In my case, the strange behaviour is that after memorization, the first call works but the second one cause a
heap exhausted
!I am using the fragment below, note that
K
is recursive: