球拍模块导入基础知识

发布于 2024-11-17 12:46:25 字数 1557 浏览 2 评论 0原文

我正在尝试在使用 Racket 时require一个文件到另一个文件中。我在同一个文件夹中有两个文件。它们是 world.rktant.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-gridgen-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-11-24 12:46:25

你的代码看起来很好,当我测试它时没有问题。

但请注意两件事:

  1. 现在最好使用 #langracket(或 #langracket/base)来启动代码。 )。这不仅已成为惯例,它还允许使用该语言提供的任何语法扩展,而 module 意味着您正在使用默认的 sexpr。 (顺便说一句,它也更方便,因为您不需要使模块名称与文件名相同。)

  2. 对模块使用 load 可能会做一些与您不同的事情我认为确实如此。最好避免使用 load,至少在您确切知道它在做什么之前。 (它与 eval 一样糟糕。)相反,您应该始终坚持使用 require。当您了解更多信息时,您会发现有时 dynamic-require 也很有用,但暂时将 load 排除在外。

Your code looks fine, and when I tested it there were no problems.

But note two things:

  1. 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, whereas module 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.)

  2. Using load with modules is probably doing something different than what you think it does. It's really best to just avoid using load, at least until you know exactly what it's doing. (It is exactly as bad as eval.) Instead, you should always stick to require. And when you learn some more, you can see that sometimes dynamic-require is useful too, but just keep load out for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文