BSL (How to Design Programs): how to import code from a separate file into definitions area?

元气小坏坏 提交于 2019-12-02 01:15:35

Note that if you're doing this for a course this strategy might not be accepted for a submission.

What I have done for some of my own projects is a pattern like this:

Have one file written in plain Racket, called "provide.rkt", like this:

; provide.rkt
#lang racket
(provide provide all-defined-out)

Then you can use this to either provide specific functions or provide all definitions from a file.

For providing specific functions

In your "library" BSL file, you can require provide into it like this, and use that to provide the specific function(s) you want:

; <auxiliary-library>.rkt
; written in BSL
(require "provide.rkt")

(provide <auxiliary-function-name>)

(define (<auxiliary-function-name> ....) ....)

And finally, in your "main" BSL file, you can require the library like this:

; written in BSL
(require "<auxiliary-library>.rkt")

(<auxiliary-function-name> ....)

For providing all definitions from a file

In your "library" BSL file, you can require provide into it and use that to provide everything:

; <auxiliary-library>.rkt
; written in BSL
(require "provide.rkt")

(provide (all-defined-out))

(define (<auxiliary-function-name-1> ....) ....)

(define (<auxiliary-function-name-2> ....) ....)

...

Then in your "main" BSL file, you can require the library and get all of the definitions:

; written in BSL
(require "<auxiliary-library>.rkt")

(<auxiliary-function-name-1> ....)

(<auxiliary-function-name-2> ....)

...

BSL is not for you. If you know how to manage modules, I recommend you use full-fledged Racket.

If you wish to create auxiliary libraries, I recommend you develop them in full Racket, provide the identifiers you need, use htdp/error to formulate error messages, and 'require' will then work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!