title: General memoization in Scheme
body: <p>i have been assigned homework to make a general memoization procedure in scheme, so far it works on procedures that take one argument, but fail on what it seems to be the last argument when provided with more than 1. It also fails to memoize procedures that take no arguments.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  
  
  <pre class="lang-scm prettyprint-override"><code>(define mem
    (lambda (mem-it func)
      (let ((table (make-table) )(func-store func))
        (cond
          ((equal? mem-it 'memoize)         
           (lambda args
             (if (null? args)
                 func
                 (let ((prev (lookup args table)))
                   (or prev
                       (let ((result (apply func args)))
                         (insert! args result table)
                         result))))))
  
          ((equal? mem-it 'unmemoize)
           (func-store))
  
          (else (display "No Such command"))))))
  </code></pre>
  
  <p>This is what i have so far </p>
  
  <pre class="lang-scm prettyprint-override"><code>(define (test-proc . args)
    (display "computing test-proc of ")
    (display args)
    (newline)
    (if (null? args)
        0
        (+ (expt (- 42 (car args)) 2)
           (apply test-proc (cdr args)))))
  </code></pre>
  
  <p>And here is the test procedure provided</p>
  
  <p>The error occurs when i try to run the following test</p>
  
  <pre class="lang-scm prettyprint-override"><code>(set! test-proc (mem 'memoize  test-proc))
  (test-proc 40 41 42 43 44)
  </code></pre>
  
  <p>Here are the other procedures used </p>
  
  <pre class="lang-scm prettyprint-override"><code>  (define (make-table)
      (list '*table*))
  
    (define (lookup key table)
      (let ((record (assoc key (cdr table))))
        (and record (cdr record))))
  
   (define (insert! key value table)
     (let ((record (assoc key (cdr table))))
       (if record
          (set-cdr! record value)
           (set-cdr! table 
                     (cons (cons key value) (cdr table))))))
  </code></pre>