【问题标题】:How do I load and use a .rkt file in command-line Racket's REPL?如何在命令行 Racket 的 REPL 中加载和使用 .rkt 文件?
【发布时间】:2020-07-10 01:46:21
【问题描述】:

我在 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 上下文中加载文件并使用它们的内容?

【问题讨论】:

  • 你是问是否可以enter!在同一个REPL中同时使用多个文件的环境?
  • 在 MIT Scheme 的 REPL 中,当我 (load "hello.scm") 和 (load "other.scm") 时,两个文件中的函数都可以立即在顶层使用。我希望能够在 Racket 中做类似的事情。

标签: file include racket external read-eval-print-loop


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多