问题
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.
回答1:
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.
回答2:
I believe you are looking for:
(include "relative/path/to/scheme/file.scm")
The (require) expression is for loading modules.
回答3:
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.
回答4:
(require "~/path-to-directory/filename")
来源:https://stackoverflow.com/questions/1207648/how-do-i-include-files-in-drscheme