Load and execute files

39 views
Skip to first unread message

Alexandre Rademaker

unread,
Mar 19, 2020, 3:58:02 PM3/19/20
to Racket Users

Suppose I have some functions defined in a file A.rkt and some tests defined in the file B.rkt. How can I execute the tests in the command line?

I was expecting to be able to run

> racket A.rkt B.rkt

But this does not evaluate the expressions on B.rkt as I was expecting!

1. Do I need to export the functions in A.rkt with (provide …)?
2. Do I need to add the (require …) in B.rkt?


The idea is to have the students submitting their A.rkt files and I could test all of them using a single set of tests in another racket file.

Ideas? What am I missing?

Alexandre

Alex Owens

unread,
Mar 19, 2020, 4:17:26 PM3/19/20
to Racket Users
Hello, this is very possible and you have the right idea.

You need to (provide ...) the functions from A.rkt in your case, and (require "A.rkt") in B.rkt. Then you can simply run "racket B.rkt" and the require will get all of the functions that have been provided in A.rkt (assuming these files are in the same directory).

If your students have many different functions in A.rkt that they would like to provide, you can use the shorthand (provide (all-defined-out)) in A.rkt to provide all of the definitions to B.rkt.

More information is available here: https://docs.racket-lang.org/guide/module-basics.html.

Ben Greenman

unread,
Mar 19, 2020, 4:45:37 PM3/19/20
to Racket Users
If A.rkt contains a few expressions (and no #lang):

```
(define a 2)
```

and B.rkt contains a few expressions:

```
(unless (= 6 a)
(error 'bad))
```

then `racket -I racket --load a.rkt --load b.rkt` evaluates the
expressions in A and then the expressions in B, as if they were all
part of the same #lang racket file

Alexandre Rademaker

unread,
Mar 19, 2020, 5:46:23 PM3/19/20
to Ben Greenman, Racket Users

Oh… That is great Ben. Thank you. This is very closer to what I am looking for, so I can avoid having to add the (provide …) in A.rkt and (require …) in B.rkt !! I should not be so hard to remove the lines `#lang racket` in all files from the students.

But I was really expecting that the option -t in the racket command should replace the explicit (require…) in B.rkt. Something like

> racket -t A.rkt -f B.rkt
default-load-handler: expected a `module' declaration, but found something else


Best,
Alexandre

Ben Greenman

unread,
Mar 19, 2020, 7:26:20 PM3/19/20
to Racket Users
> But I was really expecting that the option -t in the racket command should
> replace the explicit (require…) in B.rkt. Something like
>
>> racket -t A.rkt -f B.rkt
> default-load-handler: expected a `module' declaration, but found something
> else

Yes that almost works, but A.rkt needs to provide things for the
require to get them.

Also, I guess we need --lib racket (maybe my -I racket was a mistake):

> racket --lib racket -t A.rkt -f B.rkt

Alexandre Rademaker

unread,
Mar 19, 2020, 8:18:35 PM3/19/20
to Ben Greenman, Racket Users

Not yet. To be more concrete:

% racket --lib racket -t ex-1.2-3.rkt -t ex-1.7.rkt -t ex-1.8.rkt -r check.rkt
check.rkt:31:7: sum-of-squares-max3: unbound identifier
in: sum-of-squares-max3
location...:
check.rkt:31:7
context...:
do-raise-syntax-error
for-loop
[repeats 1 more time]
finish-bodys
lambda-clause-expander
for-loop
loop
[repeats 6 more times]
module-begin-k
expand-module16
expand-capturing-lifts
temp118_0
temp91_0
compile15
temp85_0
loop

The ex* files are the answers of one particular student. The check.rkt contains the tests. I am still trying to avoid the necessity of adding the (require …) commands in the check.rkt so I can select the student files to test in the command line. The -r or -f option cause the same error. The (provide …) forms are presented in the ex* files.

Best,
Alexandre

Ben Greenman

unread,
Mar 20, 2020, 10:08:24 AM3/20/20
to Racket Users
Does check.rkt start with a #lang line?

My B.rkt from the last message didn't have a #lang

On 3/19/20, Alexandre Rademaker <arade...@gmail.com> wrote:
>

Alexandre Rademaker

unread,
Mar 20, 2020, 12:00:53 PM3/20/20
to Ben Greenman, Racket Users

It works! Thank you. The B.rkt (or check.rkt in my last message) can’t have the `#lang racket` line. I am still confused about the implications of the `#lang racket` line in the file and the module system of Racket.

Ben Greenman

unread,
Mar 20, 2020, 12:13:48 PM3/20/20
to Racket Users
On 3/20/20, Alexandre Rademaker <arade...@gmail.com> wrote:
>
> It works! Thank you. The B.rkt (or check.rkt in my last message) can’t have
> the `#lang racket` line. I am still confused about the implications of the
> `#lang racket` line in the file and the module system of Racket.

Great!

In this case, adding #lang puts the body of B.rkt inside a new module,
which would need a require to get the definitions from the toplevel.
Reply all
Reply to author
Forward
0 new messages