问题描述
我正在使用 DrScheme 来处理 SICP,并且我注意到某些程序(例如,square
)被反复使用.我想把它们放在一个单独的文件中,这样我就可以将它们包含在其他程序中,而不必每次都重写它们,但我似乎不知道如何做到这一点.
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.
我试过了:
(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.
推荐答案
从您的问题中不清楚您使用的是什么语言级别;某些遗留语言可能会使某些机制不可用.
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:
文件使用-square.ss:
File uses-square.ss:
#lang scheme
(require "square.ss")
(define (super-duper x) (square (square x)))
文件 square.ss :
File square.ss :
#lang scheme
(provide square)
(define (square x) (* x x))
然后我可以在uses-square.ss"缓冲区上点击run",一切都会如你所愿.
Then I can hit "run" on the "uses-square.ss" buffer and everything will work the way you'd expect.
警告:未经测试的代码.
Caveat: untested code.
这篇关于如何在 DrScheme 中包含文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!