如何在 Common Lisp 中将子目录附加到路径名
我在 Common Lisp 中的路径操作(使用 SBCL)中遇到一些问题。我正在尝试将子目录名称附加到我拥有的绝对路径名中。
示例:我正在目录 #P"/home/me/somedir" 中运行我的 repl,并且我将“otherdir”作为变量,我想要的是 #P"/home/me/somedir/otherdir"
本质上,我是试图将我在 Python 中的执行方式转换为 Common Lisp: os.path.join(os.getcwd(), "otherdir")
我尝试过 (merge-pathnames (sb-posix:getcwd) "otherdir/")
但我只是拿回了 cwd。如果我尝试 (merge-pathnames "otherdir/" (sb-posix:getcwd))
我会在最后一个目录之前添加 otherdir/ : #P"/home/me/otherdir/somedir"
I也尝试过使用 (make-pathname :directory '(:relative "otherdir") :defaults (sb-posix:getcwd))
但我却得到#P“其他目录/某些目录”。
有谁知道如何在 Common Lisp 中以编程方式构建路径?
I'm having some trouble with path manipulation in Common Lisp (using SBCL). I'm trying to append a subdirectory name to an absolute pathname that I have.
Example: I am running my repl in directory #P"/home/me/somedir" and I have "otherdir" as a variable and what I want is #P"/home/me/somedir/otherdir"
Essentially, I'm trying to translate how I'd do this in Python into Common Lisp: os.path.join(os.getcwd(), "otherdir")
I've tried (merge-pathnames (sb-posix:getcwd) "otherdir/")
but I just get the cwd back. If I try (merge-pathnames "otherdir/" (sb-posix:getcwd))
I instead get otherdir/ prepended before the last directory: #P"/home/me/otherdir/somedir"
I've also tried using (make-pathname :directory '(:relative "otherdir") :defaults (sb-posix:getcwd))
but I instead get #P"otherdir/somedir".
Does anyone know how to build up a path programmatically in Common Lisp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊,路径处理之谜……你已经用
merge-pathnames
差一点就完成了,但是第二个参数必须有一个尾随的/
:=>没有尾随
/
,因此在使用otherdir/
(尾随斜杠)或otherdir
时,我们会得到 2 个意外结果:让我们使用尾随
右边的/
:那么还有更“正确”的
cwd
吗?通常,解决方案由 UIOP 给出(在 ASDF 中提供,因此总是可用的)。说实话,我以前不知道,但我查了一下:
(YMMV)
所以:
所以,
解决方案
UIOP 是一个可移植库。 SBCL 的实施情况如何?查看源代码(
M-.
):Ah, path handling mysteries… you were nearly there with
merge-pathnames
, but the second argument must have a trailing/
:=> no trailing
/
, so accordingly we get 2 unexpected results when usingotherdir/
(trailing slash) orotherdir
:Let's use a trailing
/
on the right:So is there a more "correct"
cwd
? As often, the solution is given by UIOP (shipped in ASDF, so always available).TBH I didn't know it before, but I looked it up:
(YMMV)
So:
and so,
The solution
UIOP is a portable library. What's the implementation for SBCL? Looking at the source (
M-.
):从现有路径名构造新路径名
注意:对于 Common Lisp UNIX 目录,通常在末尾有一个斜杠。
函数
PATHNAME
解析文件名字符串并返回路径名对象。在 Mac 上使用 SBCL:
或
作为 Lisp 对象的 UNIX 路径名
路径名是一个结构化对象,它还具有一个目录组件。其他组件包括:设备、主机、名称、类型、版本。路径名对象的绝对目录组件如下所示:
它是一个以关键字
:absolute
开头的列表,后跟字符串形式的目录名称。如果我们省略尾部斜杠,则 PATHNAME 函数将以不同的方式解析文件名:最后一个组成部分将是名称:
Constructing new pathnames from existing ones
Note: For Common Lisp UNIX directories usually have a slash at the end.
The function
PATHNAME
parses a filename string and returns a pathname object.Using SBCL on a Mac:
or
UNIX Pathnames as Lisp objects
A pathname is a structured object, which also has a directory component. Other components are: device, host, name, type, version. An absolute directory component of a pathname object looks like this:
It's a list beginning with the keyword
:absolute
, followed by the directory names as strings.If we omit the trailing slash, then the
PATHNAME
function will parse the filename differently: the last component will be the name: