【问题标题】:example of using external libraries or packages in Common Lisp在 Common Lisp 中使用外部库或包的示例
【发布时间】:2013-03-08 21:24:20
【问题描述】:

在 Common Lisp 中,quicklisp 是一种流行的库管理工具。我将使用该工具,并尝试使用 CL-WHO。我使用 SBCL 1.0.57 实现。我将在下面回答我自己的问题。

作为初学者,尚不清楚 ASDF 和 quicklisp 是如何实际协同工作的。因此,不清楚如何在外部源文件中实际使用您通过 quicklisp 下载的包或库。 quicklisp 常见问题解答,至少目前没有帮助。在 python 中,这非常简单:您只需输入“import somemodule”,生活就很棒。有没有 CL+quicklisp 的等价物?

如果你搜索,你会发现很多结果。以下是我发现的一些最相关的:

Lisp importing/loading file

How to use packages installed by quicklisp?

当我最初阅读这些内容时,至少想到了一个问题:如果我使用的是 quicklisp,我真的需要关心 ASDF 吗? Quicklisp 似乎是一个更高级别的管理工具。其他人建议使用 quickproject。但这真的有必要吗?

【问题讨论】:

  • quicklisp 更像是 ubuntu 中的 apt-get 之类的包管理器,而 ASDF 是 make 的类似物---不同的功能。

标签: linux package common-lisp sbcl asdf


【解决方案1】:

与 Python 导入的类比是系统定义……嗯,这是一个非常松散的类比,但是,这是要走的路。你在系统定义中声明依赖关系,然后在你期望它存在的源代码中声明它,这样如果你以后引用外部代码的位,你就这样做。

例如。在系统定义中,您可能有:(通常在my-program.asd 文件中)

(defsystem :my-program
  :version "0.0.1"
  :serial t
  :description "My program"
  :components ((:file "some-source-file"))
  ;; `some-external-package' here is the "import", i.e. here you
  ;; declared that you will be using code from this package.
  ;; ASDF will generally "know" how to get the code of that package
  ;; from here on. But if it doesn't, then there are ways to "help it"
  ;; similar to how in Python there's a procedure to prepare your local
  ;; files to be used by easy_install
  :depends-on (:some-external-package))

稍后在您的代码中,您只是假设您的程序可以使用some-external-package,例如:

(some-external-package:exported-symbol)

应该可以工作。 (“您的代码”是您在组件中指定的 some-source-file.lisp)。

这是 ASDF documentation on how to define systems

where ASDF might find it* 位置拥有此文件后,假设您已安装 ASDF(适用于您的 Lisp,SBCL 与它捆绑在一起),您将使用 (asdf:load-system :my-program) Explained here. 加载此系统

* - 一个快速的测试方法是这样做

(push "/path/to/your/system/definition/" asdf:*central-registry*)

【讨论】:

    【解决方案2】:

    通过 quicklisp 页面上的说明下载 cl-​​who 并运行:

    #!/usr/bin/sbcl --script
    
    (load "~/quicklisp/setup.lisp")
    (ql:quickload "asdf")
    (asdf:load-system 'cl-who)
    
    (with-open-file (*standard-output* "out.html" :direction :output)
                    (cl-who:with-html-output (*standard-output* nil :indent t)
                                      (:html
                                       (:head
                                        (:title "Test page"))
                                       (:body
                                        (:p "CL-WHO is really easy to use")))))
    

    对于初学者或非常懒惰的人来说,没有理由必须在顶部写 3 行而不是只写一行(就像在 python 中一样)。

    【讨论】:

    • Python 和 Common Lisp 有不同的开发和使用模型。 Common Lisp 通常不像 Python 那样用作批处理或脚本语言。您通常启动一个环境,加载支持代码,并开始调用函数。如果还有一种简化的脚本类型的方式来使用它可能会很好,但今天不是这样。
    猜你喜欢
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2019-08-14
    • 2012-01-14
    • 1970-01-01
    • 2016-10-22
    • 2011-12-24
    • 2021-05-10
    相关资源
    最近更新 更多