How to (re) upload files to Racket (X) REPL?

Suppose I have a file like

#lang racket/base (define (hello) (print "Hello")) ... more definitions ... 

and I would like to load the definitions in a file to interact with them in the (X) REPL. How to do it?

If I run (X) REPL and (load "/tmp/hello.rkt") , then the hello function is not available to me:

 -> (hello) ; hello: undefined; 

If I (require (file "/tmp/hello.rkt")) , the result will be the same. Now I can (enter! (file "/tmp/hello.rkt")) and then (hello) works, but it seems rather ... unintuitive and beginner-unfriendly.

Is this really the way it should be done, and should I just read on modules and namespaces to easily browse and experiment with my code, or is there an easier way that I skip?

NB I found How to load a file into a racket through the command line? but this only explains how to run the file. Not how to load it into REPL so that you can test / debug some specific definitions and then edit, reload, etc.

+7
source share
2 answers

Since files starting with #lang are modules, it does nothing if you load them. (He's actually doing something, but probably not something that will help you.) It's best not to use load at all, just pretend it's not there.

Now using require is the right thing, but what it does is create an instance of the module and give you access to the names it provides. In your case, you did not specify anything, which means that you cannot use your hello . To do this, you can add (provide hello) to the file. But this is most likely not what you want, as it seems like you want to debug the code. (That is, you don’t want to provide anything from your module, just work on things.)

So the right thing is enter! , or if you use xrepl, then there is a more convenient command ,en . This will create an instance of the module and force repl to use the module namespace so that you can access everything. (And you do not need to load or require it.) You can also use it several times to reload the code if you change it. But note that there were some problems with it, so you may need to install a nightly build to work with it.

Finally, you probably know this, but working with DrRacket will simplify the overall work.

+9
source

Place #lang racket/base at the top of your file, marking the file as a module form (this is # lang shorthand ); therefore downloading the file simply adds a module definition for (file "/tmp/hello.rkt") , as you find out when you need this path.

If you just want to experiment with a set of definitions and try to download them interactively, try removing #lang racket/base from the top of the file. I illustrate this here in a pair of Racket toplevel (rktl) files:

 % cat hello-unhashed.rktl (define (hello) (print "Hello") (newline)) % cat hello2-unhashed.rktl (define (hello) (print "Hello2") (newline)) % racket Welcome to Racket v5.3.2. > (load "hello-unhashed.rktl") > (hello) "Hello" > (load "hello2-unhashed.rktl") > (hello) "Hello2" > (load "hello-unhashed.rktl") > (hello) "Hello" > (exit) % 

Please note that there are many errors when working at the top level, as shown above. To get an idea of ​​what I'm talking about, try googling "The racket level at the top is hopeless" or "the top level of the plt scheme is hopeless"

+3
source

All Articles