本文介绍了Racket:执行文件并保持交互模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从命令行运行 Racket 文件并在之后保持交互模式?

Is there a way from a command line to run Racket file and stay in the interactive mode afterwards?

例如在 Python 中是一样的:

E.g. same in Python it would be:

python -i <file.py>

推荐答案

假设一个 foo.rkt 是这样的:

Assuming a foo.rkt that's this:

#lang racket
(provide x)
(define x 42)
(define y 4242)

然后你可以使用 -i 来指定交互模式(= REPL),同时使用 -t 来要求文件:

Then you can use -i to specify interactive mode (= REPL), together with -t to require the file:

$ racket -it foo.rkt
Welcome to Racket vX.X.X.
> x
42
> y
y: undefined; ...
> (exit)

请注意,y 未绑定,因为它位于模块中且未提供.更有可能的是,您想要一个位于 foo 模块内部"的 REPL,可以使用 enter! 进入模块的命名空间,或者在 REPL 中:>

Note that y is not bound since it's in the module and not provided out. More likely you want a REPL that is "inside" the foo module, which can be done using enter! to go into the module's namespace, either in the REPL:

$ racket
> (enter! "foo.rkt")
> x
42
> y
4242
> (exit)

或在命令行上,使用 -e(以及 -i 请求 REPL):

or on the command-line, using -e (and also -i to request a REPL):

$ racket -i -e '(enter! "foo.rkt")'
Welcome to Racket vX.X.X.
> x
42
> (+ x 12)
54
> (exit)

xrepl

如果您经常这样做,您可能会喜欢 xrepl.在您的 ~/.racketrc 中,只需添加:

xrepl

If you do this a lot, you'll probably like xrepl. In your ~/.racketrc simply add:

(require xrepl)

现在例子变成:

$ racket
Welcome to Racket vX.X.X.
-> ,en foo.rkt
42
"foo.rkt"> x
42
"foo.rkt"> (+ x 12)
54
"foo.rkt"> ,ex

除了 ,en 之外,XREPL 还有很多优点——比如你当前所在模块的提示指示,以及一堆其他有用的命令:

Aside from ,en, XREPL has a bunch of goodness -- like the prompt indication of the module you're currently in, as well as a bunch of other useful commands:

$ racket
Welcome to Racket vX.X.X.
-> ,h
; Available commands:
;   help (h ?): display available commands
;   exit (quit ex): exit racket
;   cd: change the current directory
;   pwd: display the current directory
;   shell (sh ls cp mv rm md rd git svn): run a shell command
;   edit (e): edit files in your $EDITOR
;   drracket (dr drr): edit files in DrRacket
;   apropos (ap): look for a binding
;   describe (desc id): describe a (bound) identifier
;   doc: browse the racket documentation
;   require (req r): require a module
;   require-reloadable (reqr rr): require a module, make it reloadable
;   enter (en): require a module and go into its namespace
;   toplevel (top): go back to the toplevel
;   load (ld): load a file
;   backtrace (bt): see a backtrace of the last exception
;   time: time an expression
;   trace (tr): trace a function
;   untrace (untr): untrace a function
;   errortrace (errt inst): errortrace instrumentation control
;   profile (prof): profiler control
;   execution-counts: execution counts
;   coverage (cover): coverage information via a sandbox
;   switch-namespace (switch): switch to a different repl namespace
;   syntax (stx st): set syntax object to inspect, and control it
;   check-requires (ckreq): check the `require's of a module
;   log: control log output
;   install!: install xrepl in your Racket init file

Emacs

但是,如果您是 Emacs 用户,您可能更喜欢使用以下内容:

Emacs

However if you're an Emacs user you might prefer using something like:

  • Geiser
  • Quack minor mode for scheme-mode
  • racket-mode (shameless self-promotion)

这篇关于Racket:执行文件并保持交互模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 05:03