Racket模块导入基础知识

dbm*_*kus 4 scheme module racket

我在require使用Racket时正在尝试使用另一个文件.我在同一个文件夹中有两个文件.他们是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 ()) )))))
Run Code Online (Sandbox Code Playgroud)

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))))))
Run Code Online (Sandbox Code Playgroud)

现在,我可以输入(require "ant.rkt")REPL,然后当我输入时(gen-cell 'none 'white)我得到错误:

reference to undefined identifier: gen-cell  
Run Code Online (Sandbox Code Playgroud)

我已查阅有关导入和导出的文档,但我无法正确导入它.我觉得这很简单,我只是不了解语法.

我应该如何更改我的代码以便我可以使用gen-gridgen-cell进行ant.rkt

Eli*_*lay 6

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

但请注意两件事:

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

  2. load与模块一起使用可能与您的想法有所不同.最好避免使用load,至少在你确切知道它在做什么之前.(这和eval.一样糟糕.)相反,你应该坚持下去require.当你学到更多东西时,你会发现它有时候dynamic-require也很有用,但load现在就不要理会了.