-3

so I'm learning elisp and wouldn't ask this if I could figure out what was wrong with just the debug message. I'm trying to implement a super basic LRU cache (with the keys in the map just mapping to themselves cuz there's no actual page data) and my function to update the cache is erroring out in a way i dont understand. This is the function:

(defun lru-retrieve ((entry-id))
  "retrieves data from cache if it exists else adds to cache"
  (
   ;; determine if cache hit
   (setq lru-cache-hit nil)
   (if (member entry-id lru-queue)
       (setq lru-cache-hit t))
   ;; if the cache is at max size, remove an entry
   (if (not (member nil lru-queue))
       (
        ;; remove an entry
        ;; if cache hit, delete requested entry from cache
        ;; else, remove last entry and last entry's data from lru-map
        (if (equal lru-cache-hit t)
            (delete entry-id lru-queue)
          ;; cache miss, delete last entry + data
          ((setq lru-cache-last-entry (car (last lru-queue)))
           ;; delete entry
           (delete lru-cache-last-entry lru-queue)
           ;; delete entry data
           (remhash lru-cache-last-entry lru-map)))
        ))
   ;; add requested entry to the front of the queue
   (setq lru-queue
         cons(entry-id lru-queue))
   ;; if cache miss, add entry data to lru-map
   (puthash entry-id entry-id lru-map)
   ;; return entry data from lru-map
   (gethash entry-id lru-map)))

the code to set up the other variables n such is:

(setq lru-length 10)
(setq lru-queue (vector lru-length nil))
(setq lru-map (make-hash-table :test 'equal)
(defun lru-examine (())
  "look at contents of LRU cache"
  (print (hash-table-keys lru-map)))

doing (lru-retrieve 1) gives me:

Debugger entered--Lisp error: (invalid-function (lambda ((entry-id)) ...

(... being the rest of the function i defined above)

I get basically the same error however i try to do this, either via executing an org-mode block or just C-x C-e in scratch. What's wrong with my function, and what can I do to figure out how my code is erroring out in a more specific way in the future?


EDIT: I made some miscellaneous fixes to the function including fixing how the argument was defined, as well as using progn to do multiple things inside a function, and am getting a slightly different error. The function now looks like this:

(defun lru-retrieve (entry-id)
  "retrieves data from cache if it exists else adds to cache"
  (progn
    ;; determine if cache hit
    (setq lru-cache-hit nil)
    (if (member entry-id lru-queue)
        (setq lru-cache-hit t))
    ;; if the cache is at max size, remove an entry
    (if (not (member nil lru-queue))
        ;; remove an entry
        ;; if cache hit, delete requested entry from cache
        ;; else, remove last entry and last entry's data from lru-map
        (if (equal lru-cache-hit t)
            (delete entry-id lru-queue)
          ;; cache miss, delete last entry + data
          (progn
            (setq lru-cache-last-entry (car (last lru-queue)))
            ;; delete entry
            (delete lru-cache-last-entry lru-queue)
            ;; delete entry data
            (remhash lru-cache-last-entry lru-map))))
    ;; add requested entry to the front of the queue
    (setq lru-queue
          cons(entry-id lru-queue))
    ;; if cache miss, add entry data to lru-map
    (puthash entry-id entry-id lru-map)
    ;; return entry data from lru-map
    (gethash entry-id lru-map)))

and I get this error running (lru-retrieve 1):

Wrong type argument: listp, [10 nil]

how can i figure out what line it doesn't like?

  • 1
    ((entry-id)) is not a good lambda list. Try (entry-id), to start with. – Drew Jun 11 at 23:06
  • 1
    n.b. Neither of the progn wrappers in your second version are needed. – phils Jun 12 at 11:54
  • "how can i figure out what line it doesn't like?" -- the debugger should be showing you a backtrace, as it clearly was the first time around. You should paste that into your question. Use M-x toggle-debug-on-error if necessary. – phils Jun 12 at 12:00
  • 2
    I've down-voted this question to avoid Community bot bumps, due to the lack of follow-up, and because the number of issues with the code suggests to me that the question will not be useful to other users. – phils Jun 17 at 12:33
  • I'm voting to close this question as off-topic because this isn't a question, it's someone wanting other people to debug for them. – DoMiNeLa10 Jun 17 at 20:37
2

When I try to evaluate the following definition

(defun mytest ((x)) "doc" (message "Hi"))

I'm getting

Debugger entered--Lisp error: (error "Malformed arglist: ((x))")

But when I change ((x)) to (x), it works.

  • so i made this change as well as using progn to wrap multiple statements in the body of the function, but i'm still getting 'invalid function' instead of anything more specific like you're getting – Abby Jun 11 at 22:18
  • 1
    OP: Show your latest code and your latest error msg. – Drew Jun 11 at 23:07
2

Parens in the wrong place:

           (setq lru-queue                                                                                                                                                                    
                 cons(entry-id lru-queue))
  • Also initialising lru-queue as a vector outside the function, and then calling member on it. – phils Jun 12 at 12:09
  • @phils: you should post that as an answer – db48x Jun 14 at 11:34
  • Done; although I suspect it is redundant. – phils Jun 17 at 12:31
1

The error from the most recent edit to the question, Wrong type argument: listp, [10 nil], will be due to initialising (setq lru-queue (vector lru-length nil)) and then calling (member entry-id lru-queue); as member requires a list argument, and lru-queue is a vector.

Not the answer you're looking for? Browse other questions tagged or ask your own question.