如何在当前 Racket 文件中包含另一个定义文件? [英] How to include another file of definitions in current Racket file?

查看:44
本文介绍了如何在当前 Racket 文件中包含另一个定义文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了:

#lang racket
(require foo.rkt)

#lang racket
(require "foo.rkt")

#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.

推荐答案

你的第二个猜测实际上是正确的:

Your second guess is actually correct:

#lang racket
(require "foo.rkt")

不过,你还需要做的是provide 你想从另一个文件中要求的函数,否则没有来自 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.

foo.rkt 文件的一个例子是:

#lang racket ; foo.rkt
(provide x)
(define x 5)

(提供的位置无关紧要,可以在 define 语句的上方或下方.)

(The location of the provide does not matter, and can be above or below the define statement.)

如果需要,您可以使用 all-defined-out 一次性导出模块可以提供的所有内容.为此:

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)

现在你可以要求这个文件并在另一个模块中使用 xy :

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")

作为第一个附录,Racket 还有 importload.一般来说,你不想要这些,通常应该坚持 require/provide 对.

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.

作为第二个附录,您创建的本地文件将作为字符串传递到 require.当它是一个符号时,例如:(require pict),这意味着您需要一个已安装的模块.虽然它更高级,但您可以通过阅读关于集合的文档.

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.

这篇关于如何在当前 Racket 文件中包含另一个定义文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆