gpt4 book ai didi

file - 如何在命令行 Racket 的 REPL 中加载和使用 .rkt 文件?

转载 作者:行者123 更新时间:2023-12-05 05:04:42 26 4
gpt4 key购买 nike

我在 Ubuntu 18.04 上使用 Racket 7.6。我创建了这个文件,hello.rkt:

#lang racket

(define (hello) 'hello-world)
(hello)

然后我调用它:

> racket hello.rkt
'hello-world

不错。接下来我尝试将代码加载到 REPL 中并使用它:

> racket -i hello.rkt
Welcome to Racket v7.6.
> (hello) ; the function is unavailable here
; hello: undefined;
; cannot reference an identifier before its definition
; in module: top-level
; [,bt for context]
> (load "hello.rkt") ; load gives no error, but ...
> (hello) ; the function is unavailable here
; hello: undefined; ...
> (require "hello.rkt") ; require gives no error ...
'hello-world ; and runs (hello), but ...
> (hello) ; the function is unavailable here
; hello: undefined; ...
> (include "hello.rkt") ; include gives no error, but ...
> (hello) ; the function is unavailable here
; hello: undefined; ...
> (enter! "hello.rkt") ; enter! gives no error, but ...
"hello.rkt"> (enter! "other.rkt") ; if I enter! another file ...
"other.rkt"> (hello) ; the hello function is unavailable here
; hello: undefined; ...

简而言之:如何加载文件并在顶层命令行 REPL 上下文中使用它们的内容?

最佳答案

根据 https://docs.racket-lang.org/guide/intro.html , 你可以通过省略 #lang 来“模仿传统的 Lisp 环境”声明和使用(load <file>)在 REPL 中。当我从文件中删除 #lang 行时,我得到了这个交互:

> racket
Welcome to Racket 7.6.
> (load "hello.rkt")
'hello-world
> (hello)
'hello-world

该页面确实“强烈建议反对”这种做法,转而支持基于模块的代码。

关于file - 如何在命令行 Racket 的 REPL 中加载和使用 .rkt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60917877/

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