Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including files from within racket/scheme

Tags:

scheme

racket

I'm trying to use drracket to work thru the exercises in "How To Design Programs 2nd Ed".

A number of the exercises in this build up on the answers to previous questions, so I would like to include the source files from the answered questions so that I don't have to copy and paste the the body of the old answer each time.

My main question is: How do I do this?

I have looked thru the documentation and found a method called include that seems to do what I want, but I cant work out how to use it correctly.

eg - I have two files:

test.rkt - this compiles and runs fine and contains one function:

(define (test) 1)
(test)

newtest.rkt - I would like this file to 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 that's all the information I'm given...)

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

like image 942
Dave Smylie Avatar asked Feb 21 '23 17:02

Dave Smylie


1 Answers

The include form isn't working 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 #reader.... bit is what generates the module. But when that gets included into the other file, it doesn't make sense. Thus the error complaining about module.

Unfortunately, as far as I can tell, the HtDP languages still don't have provide, which is what you need to make this work right.

If you really want to get this working, here's a way to hack around it:

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

#lang racket
(provide provide)

(That declares a module using the full Racket language that provides only the built-in special form provide.)

Add the following lines to your "test.rkt" program. (Make sure DrRacket's Language is set back to "Beginning Student" or whichever teaching language you're using for this.)

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

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

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

(require "test.rkt")

That imports everything provided by "test.rkt": currently just test, but you can add other things to, you just have to provide them.

like image 76
Ryan Culpepper Avatar answered Feb 27 '23 11:02

Ryan Culpepper