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)

  • 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? – Silvio Mayolo Dec 27 '20 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](http://clhs.lisp.se/Body/v_most_1.htm) – giovannibrumana Dec 27 '20 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. – Silvio Mayolo Dec 27 '20 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 ...`) – Renzo Dec 27 '20 at 17:36
  • It feels like the error is somewhere else in my code, but thank you for help anyway! – giovannibrumana Dec 27 '20 at 17:38
  • why not just use DOLIST instead of a complex recursive function? – Rainer Joswig Dec 27 '20 at 17:49
  • @RainerJoswig I'm developing this project for coursework, and I can't use a lot of functions (including that one) – giovannibrumana Dec 27 '20 at 18:43
  • what is the AND doing? – Rainer Joswig Dec 28 '20 at 10:18
  • I use it as a way to execute both instructions (setf and sample). – giovannibrumana Dec 29 '20 at 14:05

1 Answers1

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.

AlbusMPiroglu
  • 645
  • 3
  • 13
  • 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... – giovannibrumana Dec 31 '20 at 21:10