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

How do I load and use a .rkt file in command-line Racket's REPL?

我在 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,您可以 "imitate a traditional Lisp environment" 通过省略 #lang 声明并在 REPL 中使用 (load <file>)。当我从文件中删除 #lang 行时,我得到了这个交互:

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

页面 执行 "strongly recommend against" 实践,转而支持基于模块的代码。