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

3

我使用的是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; ...

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

你是在询问是否可以在同一个REPL中一次性进入多个文件的环境吗? - Alex Knauth
在MIT Scheme的REPL中,当我执行(load "hello.scm")和(load "other.scm")时,两个文件中的函数立即可用于toplevel。我希望能够在Racket中做类似的事情。 - Tony
1个回答

2
根据https://docs.racket-lang.org/guide/intro.html,您可以通过在REPL中省略#lang声明并使用(load <file>)来“模拟传统的Lisp环境”。当我从文件中删除#lang行时,会得到以下交互:
> racket
Welcome to Racket 7.6.
> (load "hello.rkt")
'hello-world
> (hello)
'hello-world

该页面“强烈反对”这种做法,而更青睐于基于模块的代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接