Including files from a rocket / circuit

I am trying to use drracket to work through exercises in "How To Design Programs 2nd Ed".

A series of exercises in this is expressed in the answers to previous questions, so I would like to include the source files from the answered questions so that I would not have to copy and paste the body of the old answer every time.

My main question is: how to do this?

I went through the documentation and found a method called include , which seems to do what I want, but I cannot figure out how to use it correctly.

eg - I have two files:

test.rkt - it compiles and works fine and contains one function:

 (define (test) 1) (test) 

newtest.rkt . I would like this file to be able to use the function defined in test.rkt.

 (require racket/include) (include "test.rkt") (define (newtest) (* test 2)) 

When I try to compile this, I get the following error:

 module: this function is not defined 

(Not very informative, but all the information was given to me ...)

How do I include this first file without getting this error? Is include even the right function for this, or is my approach completely wrong?

+4
source share
1 answer

The include form does not work, because when the language is set to "Beginning Student" or one of the other teaching languages, DrRacket actually wraps your program in a module. You can see this if you open "test.rkt" in a regular text editor. The bit #reader.... is what the module generates. But when it gets included in another file, it makes no sense. Thus, the error causing complaints about module .

Unfortunately, as far as I can tell, in the HtDP languages ​​there is still no provide what you need to work properly.

If you really want this to work, here is a way to hack it:

Create a new file called "provide.rkt" in the same directory as your other files. While you are editing this file (and only this file), set the language in DrRacket to "Detect language from source". Put the following two lines in "provide.rkt" :

 #lang racket (provide provide) 

(Declares a module using the full Racket language, which provides only the built-in special form provide .)

Add the following lines to your "test.rkt" program. (Make sure that DrRacket Language is set to "Beginning Student" or what training language you use for this.)

 (require "provide.rkt") (provide test) 

Now "test.rkt" is the module that exports your test function. (It was always a module, it just didn’t have an export before, so it wasn’t very useful.)

Add the following lines to your "newtest.rkt" program:

 (require "test.rkt") 

This imports everything that is provided by "test.rkt" : it is currently just a test , but you can add other things, you just need to provide them.

+8
source

Source: https://habr.com/ru/post/1392820/


All Articles