球拍模块导入基础知识
我正在尝试在使用 Racket 时require
一个文件到另一个文件中。我在同一个文件夹中有两个文件。它们是 world.rkt
和 ant.rkt
。
world.rkt
:
(module world racket
(provide gen-grid gen-cell)
(define (gen-cell item fill)
(cons item fill))
(define (gen-grid x y fill)
(begin
(define (gen-row x fill)
(cond ((> x 0) (cons (gen-cell (quote none) fill)
(gen-row (- x 1) fill)))
((<= x 0) (quote ()) )))
(cond ((> y 0) (cons (gen-row x fill)
(gen-grid x (- y 1) fill)))
((<= y 0) (quote ()) )))))
ant.rkt
:
(module ant racket
(require "world.rkt")
(define (insert-ant grid x y)
(cond ((> y 0) (insert-ant (cdr grid) x (- y 1)))
((< y 0) 'Error)
((= y 0) (begin
(define y-line (car grid))
(define (get-x line x)
(cond ((> x 0) (get-x (cdr line) (- x 1)))
((< x 0) 'Error)
(= x 0) (gen-cell 'ant (cdr (car line))) ))
(get-x y-line x))))))
现在,我可以在 REPL 中输入 (require "ant.rkt")
,然后当我输入 (gen-cell 'none 'white)
我收到错误:
reference to undefined identifier: gen-cell
我已查找有关导入和导出的文档,但无法正确导入它。我觉得这很简单,我只是不明白语法。
我应该如何更改代码才能在 ant.rkt
中使用 gen-grid
和 gen-cell
?
I'm trying to require
one file in another while working with Racket. I have two files in the same folder. They are world.rkt
and ant.rkt
.
world.rkt
:
(module world racket
(provide gen-grid gen-cell)
(define (gen-cell item fill)
(cons item fill))
(define (gen-grid x y fill)
(begin
(define (gen-row x fill)
(cond ((> x 0) (cons (gen-cell (quote none) fill)
(gen-row (- x 1) fill)))
((<= x 0) (quote ()) )))
(cond ((> y 0) (cons (gen-row x fill)
(gen-grid x (- y 1) fill)))
((<= y 0) (quote ()) )))))
ant.rkt
:
(module ant racket
(require "world.rkt")
(define (insert-ant grid x y)
(cond ((> y 0) (insert-ant (cdr grid) x (- y 1)))
((< y 0) 'Error)
((= y 0) (begin
(define y-line (car grid))
(define (get-x line x)
(cond ((> x 0) (get-x (cdr line) (- x 1)))
((< x 0) 'Error)
(= x 0) (gen-cell 'ant (cdr (car line))) ))
(get-x y-line x))))))
Now, I can type (require "ant.rkt")
in the REPL, and then when I type (gen-cell 'none 'white)
I get the error:
reference to undefined identifier: gen-cell
I've looked up documentation on imports and exports, but I can't get it to import correctly. I feel like it's something really simple that I just don't understand about the syntax.
How should I change my code so that I can use gen-grid
and gen-cell
in ant.rkt
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码看起来很好,当我测试它时没有问题。
但请注意两件事:
现在最好使用
#langracket
(或#langracket/base
)来启动代码。 )。这不仅已成为惯例,它还允许使用该语言提供的任何语法扩展,而module
意味着您正在使用默认的 sexpr。 (顺便说一句,它也更方便,因为您不需要使模块名称与文件名相同。)对模块使用
load
可能会做一些与您不同的事情我认为确实如此。最好避免使用load
,至少在您确切知道它在做什么之前。 (它与 eval 一样糟糕。)相反,您应该始终坚持使用 require。当您了解更多信息时,您会发现有时dynamic-require
也很有用,但暂时将load
排除在外。Your code looks fine, and when I tested it there were no problems.
But note two things:
It is much better these days to start your code with
#lang racket
(or#lang racket/base
). Not only has this became the convention, it allows using whatever syntactic extensions the language provides you with, whereasmodule
means that you're using the default sexpr. (BTW, it's also more convenient in that you don't need to make the module name be the same as the file name.)Using
load
with modules is probably doing something different than what you think it does. It's really best to just avoid usingload
, at least until you know exactly what it's doing. (It is exactly as bad aseval
.) Instead, you should always stick torequire
. And when you learn some more, you can see that sometimesdynamic-require
is useful too, but just keepload
out for now.