Reputation: 33
I've tried:
#lang racket
(require foo.rkt)
and
#lang racket
(require "foo.rkt")
and
#lang racket
(require (relative-in "." foo.rkt))
If a straightforward example to just include one file in the current file's directory exists in the documentation, I cannot find it. Please help.
Upvotes: 3
Views: 1736
Reputation: 22332
Your second guess is actually correct:
#lang racket
(require "foo.rkt")
However, what you need to do is also provide
the functions you would like to require from the other file, otherwise no variables from foo.rkt
will be bound in your module.
So an example of a foo.rkt
file would be:
#lang racket ; foo.rkt
(provide x)
(define x 5)
(The location of the provide does not matter, and can be above or below the define
statement.)
If you want, you can use all-defined-out
to export everything a module can provide in one go. To do this:
#lang racket ; foo.rkt
(provide (all-defined-out))
(define x 5)
(define y 6)
Now you can require this file and use x
and y
in another module:
#lang racket
(require "foo.rkt")
x ; => 5
Note that the two files need to be in the same directory, otherwise you will need to pass in the path to that directory. Such as:
(require "subdir/to/foo.rkt")
As a first addendum, Racket also has import
and load
. In general, you do not want these, and should generally stick to the require
/provide
pair.
As an second addendum, local files that you create are passed into require
as a string. When its a symbol, such as in: (require pict)
, that means you are requiring an installed module. While its more advanced you can make one of those by reading the documentation on collections.
Upvotes: 7