如何在LISP中成为读者宏来阅读原始字符串(即后斜线是字面意思不是逃生字符)

发布于 2025-02-14 00:35:26 字数 125 浏览 0 评论 0原文

写下此r“ c:\ data”

在Python中,您可以在LISP中 ?即,我想要一个具有相同功能的读取器宏,因此我不再需要在Windows路径中逃脱后斜线。我不想使用前向斜线,因为Windows中的大量命令不了解它们。

In python you can write this r"c:\data"

How do I the same in lisp? ie I want a reader macro that has the same functionality so I no longer need to escape backslashes in windows paths. I do not want to use forward slashes as plenty of commands in windows dont understand them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2025-02-21 00:35:27

例如

(defun read-literal-string (stream delimiter arg)
  (declare (ignore arg))
  (loop for char = (read-char stream nil stream)
        when (eq char stream)
          do (error "hit end of stream")
        until (char= char delimiter)
        collect char into chars
        finally (return (coerce chars 'string))))

(set-dispatch-macro-character #\# #\" #'read-literal-string)

,现在

> #"fo\o"
"fo\\o"

> #"foo\"
"foo\\"

显然将更仔细地写一个真实的版本

For instance

(defun read-literal-string (stream delimiter arg)
  (declare (ignore arg))
  (loop for char = (read-char stream nil stream)
        when (eq char stream)
          do (error "hit end of stream")
        until (char= char delimiter)
        collect char into chars
        finally (return (coerce chars 'string))))

(set-dispatch-macro-character #\# #\" #'read-literal-string)

And now

> #"fo\o"
"fo\\o"

> #"foo\"
"foo\\"

obviously a real version would be more carefully written

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文