gpt4 book ai didi

load - 在 Common Lisp 中加载文件时读取下一行

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:52 28 4
gpt4 key购买 nike

我有一个文件,test.lisp,本质上是这样的:

(load (compile-file "init.lisp"))
(my-funcA 2 3)
(my-funcB 4 5)
; bunch more lines like the last ones

在 init.lisp 文件中,我希望能够读取 (my-funcA 2 3)、(my-funcB 4 5) 等行,并用它做一些事情。这可能吗?

我试过使用:

(let ((input (read)))
; do stuff here
)

在 init.lisp 文件中,但这只是一直等待来自键盘的输入,而不是从正在加载的文件中读取。任何帮助将不胜感激!

最佳答案

LOAD 不会将 *STANDARD-INPUT*(或任何其他标准流变量)绑定(bind)到它正在加载的文件,因此您需要自己执行此操作。

(defun load-with-stdin (filename) {
(let ((eof '(:eof)))
(with-open-file (*standard-input* filename)
(loop for expr = (read *standard-input* nil eof)
while (not (eq expr eof))
do (eval expr)))))

(load-with-stdin "test.lisp")

但是,这似乎是一种奇怪的做事方式。为什么不直接在 init.lisp 中定义一个函数,然后调用它:

(load (compile-file "init.lisp")) ;; defines some-func
(some-func '(my-funcA 2 3))
(some-func '(my-funcB 4 5))

关于load - 在 Common Lisp 中加载文件时读取下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30792696/

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