Racket: execute file and stay in interactive mode
Asked Answered
C

3

17

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

E.g. same in Python it would be:

python -i <file.py>
Counterpane answered 9/11, 2013 at 11:3 Comment(0)
B
33

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:

Branton answered 9/11, 2013 at 14:12 Comment(5)
Thanks @Eli Barzilay for the edit to the start of my answer. I'm so used to XREPL that my example of "not" using it... actually was using it. Your edit makes even clearer that enter!-ing a module is the key idea here. As for XREPL, whoever created that is brilliant. ;)Branton
If you have racket-minimal installed, you'll need to do a raco pkg install --auto xrepl to get going with xrepl. And then go grab lunch while it satisfies all the dependencies. Maybe going the minimal route isn't worth the savings of the full racket install.Limen
Since Racket 6.7, note that (from the docs for "Init Libraries"): The default interactive module starts xrepl and runs the (find-system-path 'init-file) file in the users home directory.Ribaldry
on racket 7.3, the following command doesn't work racket -i -e '(enter! "foo.rkt")' it will report the error which x is undefinedFlay
Since Racket 7.3 includes xrepl I suggest following that section of the answer -- simply use ,en foo.rkt.Branton
L
2

If you are using Visual Studio Code as an editor, you may want to use the "Code Runner extension"
make sure it's installed from the vs code marketplace
then enter Preferences: Open Settings (JSON) and past the following:

"code-runner.executorMap": {
        "racket": "(exit); racket -i -e '(enter! \"$fileName\")'",
    },

You will be able to run directly your file by clicking the Run Code icon or by pressing Ctrl+Alt+N

NB: the same manouvre goes for "scheme" since it's interpreted by racket as well, however putting #lang racket in the top of your file is necessary

Longwinded answered 11/3, 2021 at 0:8 Comment(0)
C
0

It can be done with

racket -if <file.rkt>

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

#lang racket
Counterpane answered 9/11, 2013 at 11:15 Comment(1)
-f is going to do a load -- and load is an extremely bad idea, even more so as a recommendation to newbies.Hitch

© 2022 - 2024 — McMap. All rights reserved.