链接文件包含

时间:2014-01-31 21:05:31

标签: racket

我有一个文件configuration.rkt: (定义文件路径“path / to / file.rkt”)

然后我有'main'文件program.rkt,其中我想要包含file-path指定的文件。我试过这种方式,但它不起作用:

(include "configuration.rkt")
(include file-path)

我该怎么办?

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

正如@soegaard所说,你想通过require使用Racket的模块系统,而不是文本包含源文件。它会为你节省大量的悲伤。

我注意到您似乎希望在运行时确定源文件的选择?

如果是这样,有两个选择:

  1. 您可以使用units and signatures

  2. 您可以使用dynamic-require。例如:

  3. config.rkt:

    #lang racket
    
    (provide to-be-required)
    
    (define to-be-required "foo.rkt")
    

    foo.rkt:

    #lang racket
    
    (provide foo-value
             foo-proc)
    
    (define foo-value "I am from foo.rkt and I was dynamic-required!")
    
    (define (foo-proc)
      "I am foo-proc.")
    

    main.rkt

    #lang racket
    
    (require "config.rkt")
    
    (printf "Let's dynamic-require ~a, as config.rkt said.\n" to-be-required)
    
    (define foo-value (dynamic-require to-be-required 'foo-value))
    (define foo-proc  (dynamic-require to-be-required 'foo-proc))
    
    foo-value
    (foo-proc)
    

    输出:

    Let's dynamic-require foo.rkt, as config.rkt said.
    "I am from foo.rkt and I was dynamic-required!"
    "I am foo-proc."