自定义函数名称中的冒号导致错误

问题描述 投票:0回答:2

我是Lisp的新手,目前正在尝试在Common Lisp中重做旧的UCI Lisp程序。复制以下代码(在funcs.lisp中)时出现以下问题:

;;; HEADER-CD gets the head act of a CD form.
(defun header:cd 
    (x) 
    (car x))

解释器发出此错误:

- READ from #<INPUT BUFFERED FILE-STREAM CHARACTER #P"funcs.lisp" @11>: there is no package
  with name "HEADER"

我不了解代码中冒号的用途,但我猜想它是要指定可接受输入的类型,因为还有另一个函数称为“ header:pair”。

我不确定该如何解决。也许我可以改用UCI Lisp,但找不到其编译器/解释器。请帮助。

lisp common-lisp
2个回答
0
投票

冒号用于指定符号包。在您的情况下,这意味着函数cd应该在包头中。您应该执行以下操作:

(defpackage header) ; this creates the package header
(in-package :header) ; you move into that package so that it now becomes your working space
(defund cd (x)  ; define the function in header
  (car x) )
(export 'cd) ; this allows the function cd to be called from another package
(in-package :cl) ; now move to another package, i.e. the standard CL package
(header:cd '(1 2 3)) => 1 ; call the function cd from the other package

有点费解,但是当您学习软件包时,一切都会变得更加清晰。


0
投票

我建议您以一种更加“通用的方式”重新排列数据结构。

例如,您可以使用结构:

(defstruct cd header track-list)

CL-USER> (let ((cd (make-cd
                    :header "my cd 1"
                    :track-list (list "track 1" "track 2"))))
           (cd-header cd))
;;=> "my cd 1"
© www.soinside.com 2019 - 2024. All rights reserved.