0

I'm writing a function in Common Lisp, but I'm getting this error I mentioned in the title.

(defun sample (graph-id vertices)
    (cond
        ((null vertices) nil)
        (t 
            (and
                (setf 
                    (gethash (list graph-id (first vertices)) *keys*)
                    MOST-POSITIVE-DOUBLE-FLOAT)
                (sample graph-id (rest vertices))))))

In particular, when the compiler reaches the (gethash (list graph-id (first vertices)) *vertex-keys*) line, if I swap graph-id (first vertices) with (first vertices) graph-id, the error disappears. It also disappears if I use second, third or any other nth functions rather than first, I can't understand why it happens. vertices is a list like (A B C D E F)

9
  • I'm calling this as (sample 0 '(A B C D E F)) with *keys* defined to be an empty hash table, and i get no errors on CLISP or SBCL. How are you calling this, and what's the value of the relevant global variable? Commented Dec 27, 2020 at 17:24
  • @SilvioMayolo thanks for replying. I'm calling the function with (sample id '(A B C D E F)), where id is equal to 'grafo. The MOST-POSITIVE-DOUBLE-FLOAT is a constant defined in Lisp I guess, as it says here link Commented Dec 27, 2020 at 17:33
  • 1
    Hm... unfortunately it looks like there must be some other piece of code that's messing with your environment, as I still can't reproduce your error with those exact inputs. Consider trying to run this in a clean environment with no other code preceding it. Commented Dec 27, 2020 at 17:34
  • 1
    I cannot reproduce the error, either. The error message seems caused by problems with parenthesis around the branch of cond (for instance by something like ((t ...) Commented Dec 27, 2020 at 17:36
  • It feels like the error is somewhere else in my code, but thank you for help anyway! Commented Dec 27, 2020 at 17:38

1 Answer 1

0

I feel like your problem is related to how you construct the hash table *key*. The way gethash works is, it uses the hash table's designated :test function (given during make-hash-table call).

Now, given that you're setting a value to a key that's a list each time in the recursion, I doubt you'll add anything to your hash table if it uses default make-hash-table (which uses eql for testing), because each time your (gethash (list graph-id (first vertices)) *keys*) runs, the (list ..) call creates a list that's never eql to any previous list.

If you haven't already, change your possibly:

(defvar *key* (make-hash-table)) 

line to something like

(defvar *key* (make-hash-table :test #'equal)) 

and things should work better.

Sign up to request clarification or add additional context in comments.

1 Comment

First of all, thanks for helping. My hash table is already declared as you suggested. Anyway, I can't explain why because I don't think I changed anything related to that part of code, but my error seems to be disappeared. It feels like the complier had been messing up with some code that wasn't really connected with the error that pushed me out, but this is just my guessing...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.