Racket 球拍:执行文件并保持交互模式
有没有办法从命令行运行Racket文件并在之后保持交互模式 例如,在Python中也是这样:Racket 球拍:执行文件并保持交互模式,racket,Racket,有没有办法从命令行运行Racket文件并在之后保持交互模式 例如,在Python中也是这样: python -i <file.py> python-i 可以用 racket -if <file.rkt> 假设一个foo.rkt就是这样: #lang racket (provide x) (define x 42) (define y 4242) 然后,您可以使用-i指定交互模式(=REPL),并使用-t要求文件: $ racket -it foo.rkt Welco
python -i <file.py>
python-i
可以用
racket -if <file.rkt>
假设一个
foo.rkt
就是这样:
#lang racket
(provide x)
(define x 42)
(define y 4242)
然后,您可以使用-i
指定交互模式(=REPL),并使用-t
要求文件:
$ racket -it foo.rkt
Welcome to Racket vX.X.X.
> x
42
> y
y: undefined; ...
> (exit)
请注意,y
未绑定,因为它位于模块中而未提供。您更可能希望REPL位于foo
模块的“内部”,可以使用enter
进入模块的命名空间,在REPL中:
$ racket
> (enter! "foo.rkt")
> x
42
> y
4242
> (exit)
或者在命令行上,使用-e
(以及-i
请求REPL):
xrepl
如果您经常这样做,您可能会喜欢xrepl
。在~/.racketrc
中,只需添加:
(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还有一系列优点——比如提示您当前所在的模块,以及一系列其他有用的命令:
$ 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用户,您可能更喜欢使用以下内容:
- 方案模式的次要模式
- (无耻的自我推销)
确保它是从vs代码市场安装的
然后输入
首选项:打开设置(JSON)
并跳过以下内容:"code-runner.executorMap": {
"racket": "(exit); racket -i -e '(enter! \"$fileName\")'",
},
通过单击runcode
图标或按Ctrl+Alt+N
NB:同样的手法也适用于“scheme”,因为它也是由racket解释的,但是将
#lang racket
放在文件的顶部是必要的-f
将进行加载
——而加载
是一个非常糟糕的主意,作为对新手的推荐,更是如此。感谢@Eli Barzilay对我答案开头的编辑。我太习惯XREPL了,以至于我的“不”使用它的例子。。。实际上是在用它。您的编辑使enter更清晰代码>-这里的关键思想是创建模块。至于XREPL,无论是谁创造了它都是辉煌的如果安装了racket minimal,则需要执行raco pkg安装--auto-xrepl
才能开始使用xrepl。然后在满足所有依赖关系的时候去吃午饭。也许使用最小路径不值得为整个racket安装节省成本。从racket 6.7开始,请注意(从“Init库”的文档中):默认交互模块启动xrepl并运行用户主目录中的(查找系统路径“Init file”)文件。在racket 7.3上,以下命令无效racket-i-e'(输入!“foo.rkt”)
它将报告x未定义的错误,因为Racket 7.3包括xrepl
我建议按照答案的这一部分操作——只需使用,en foo.rkt
。
$ 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
"code-runner.executorMap": {
"racket": "(exit); racket -i -e '(enter! \"$fileName\")'",
},