gpt4 book ai didi

serialization - 如何从 SBCL 解释器将特定函数写入文件?

转载 作者:行者123 更新时间:2023-12-01 09:00:10 25 4
gpt4 key购买 nike

假设我在没有 SLIME 的情况下玩了一些 SBCL,没有任何东西,简单的解释器。现在我想将几个函数保存在一个文件中。不是核心图像,只是文本形式的一些代码。我该怎么做?

最佳答案

有两种方法可以做到这一点:使用 DRIBBLE 和/或 FUNCTION-LAMBDA-EXPRESSION

首先是在实验之前总是使用Common Lisp函数DRIBBLE:

rjmba:tmp joswig$ sbcl
This is SBCL 1.1.9, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.

Dribble 采用文本文件的路径名。一旦调用,交互式 IO 将被写入该文件。

* (dribble "/Lisp/tmp/2013-09-06-01.text")

* (defun foo (a) (1+ a))

FOO
* (foo 10)

11
* (quit)

查看文件:

rjmba:tmp joswig$ cat 2013-09-06-01.text 

* (defun foo (a) (1+ a))

FOO
* (foo 10)

11
* (quit)

从上面你应该能够看到你是否输入了任何有趣的功能......您还可以设置您的 SBCL(例如使用 init 文件)以始终在开始时设置 dribble。不带参数调用 (dribble) 会结束 dribble

下一步:FUNCTION-LAMBDA-EXPRESSION:

* (defun foo (b) (1- b))

FOO

现在您可以调用 FUNCTION-LAMBDA-EXPRESSION 来获取定义。它可能会稍作改动,但它应该可以恢复以代码形式编写的有值(value)的想法:

* (function-lambda-expression #'foo)

(SB-INT:NAMED-LAMBDA FOO
(B)
(BLOCK FOO (1- B)))
NIL
FOO

关于serialization - 如何从 SBCL 解释器将特定函数写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18650988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com