Вопрос

I've set up Quicklisp to run whenever SBCL runs, and added the following line to the top of my file that I'm trying to use the priority-queue library in (as suggested in the answer to my earlier question, Priority queue for Common Lisp?). However, when I try to use it, I get errors from SBCL, saying that the functions from priority-queue are not defined! What am I missing?

For reference, I tried to write something like this:

(ql:quickload "priority-queue")

(defparameter *heap* (make-pqueue #'<))

And I get an error saying that make-pqueue is not defined.

Это было полезно?

Решение

In common lisp, anything that's named (a variable, a function, a macro) is attached to a symbol. In this case, you have a function which is attached to the symbol make-pqueue. Symbols are separated from each other using packages. This keeps collisions to a minimum and also allows for things like internal variables/functions that aren't exported by the package.

Sounds like you need to do one of three things:

  1. Use the package name before the function: (priority-queue:make-pqueue #'<). This method is good if you want people reading your source to know exactly what code is being run. however, it can get cumbersome if you call the package many times.
  2. Use the priority-queue package in the current package you're in:

    (use-package :priority-queue)
    (make-pqueue #'<)
    

    What this does is import every exported symbol from the priority-queue package into the current package you're in (most likely cl-user). While this is good for testing, you generally want to create your own package. See next item.

  3. Define your own package that uses priority-queue:

    (defpackage :queue-test (:use :cl :priority-queue))
    (in-package :queue-test)
    (make-pqueue #'<)
    

Defining your own packages seems like a lot of work at first, but you'll start to like the separation you get, especially if you start integrating different pieces of your code together.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top