Racket: execute file and stay in interactive mode

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 05:22:31

Assuming a foo.rkt that's this:

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

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)

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)

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

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

(require xrepl)

Now the example becomes:

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

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

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

It can be done with

racket -if <file.rkt>

However it will not work as expected if the file starts with

#lang racket
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!