views:

354

answers:

5

I'm using DrScheme to work through SICP, and I've noticed that certain procedures (for example, square) get used over and over. I'd like to put these in a separate file so that I can include them in other programs without having to rewrite them every time, but I can't seem to figure out how to do this.

I've tried:

(load filename)
(load (filename))
(load ~/path-to-directory/filename)
(require filename)
(require ~/path-to-directory/filename)
(require path-from-root/filename)

None of these works. Obviously I'm grasping at straws -- any help is much appreciated.

A: 

I believe you're looking for include:

(include path-spec)

Documentation here: http://docs.plt-scheme.org/reference/include.html

Corbin March
A: 

I believe you are looking for:

(include "relative/path/to/scheme/file.scm")

The (require) expression is for loading modules.

Michael Aaron Safyan
A: 

In MIT/GNU Scheme, you can load a file with something like this:

(load "c:\\sample-directory\\sample-file.scm")

But I do not know if it works in DrScheme.

Poorya
it works in DrScheme if the language is set to SIPC, using the SICP add-in
dan
A: 
(require "~/path-to-directory/filename")
troelskn
+1  A: 

It's not clear from your question what language level you're using; certain legacy languages may make certain mechanisms unavailable.

The best inclusion/abstraction mechanism is that of modules.

First, set your language level to "module". Then, if I have these two files in the same directory:

File uses-square.ss:

#lang scheme

(require "square.ss")

(define (super-duper x) (square (square x)))

File square.ss :

#lang scheme

(provide square)

(define (square x) (* x x))

Then I can hit "run" on the "uses-square.ss" buffer and everything will work the way you'd expect.

Caveat: untested code.

John Clements