gpt4 book ai didi

Racket:执行文件并保持交互模式

转载 作者:行者123 更新时间:2023-12-03 15:32:13 24 4
gpt4 key购买 nike

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

例如。在Python中相同的是:

python -i <file.py>

最佳答案

假设 foo.rkt就是这样:

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

然后你可以使用 -i-t 一起指定交互模式 (= REPL)要求文件:
$ racket -it foo.rkt
Welcome to Racket vX.X.X.
> x
42
> y
y: undefined; ...
> (exit)

请注意 y不受约束,因为它在模块中并且没有提供。您更有可能想要一个位于 foo 内部的 REPL模块,可以使用 enter!在 REPL 中进入模块的命名空间:
$ racket
> (enter! "foo.rkt")
> x
42
> y
4242
> (exit)

或在命令行上,使用 -e (还有 -i 来请求 REPL):
$ racket -i -e '(enter! "foo.rkt")'
Welcome to Racket vX.X.X.
> x
42
> (+ x 12)
54
> (exit)

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 用户,您可能更喜欢使用以下内容:
  • Geiser
  • Quack方案模式的次要模式
  • racket-mode (无耻自吹自擂)
  • 关于Racket:执行文件并保持交互模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19875182/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com