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

前端 未结 2 342
别跟我提以往
别跟我提以往 2021-01-21 07:05

I\'m having an issue with BSL. I want to divide my code into separate auxiliary files and use

(require \"auxiliary-function.rkt\") 

at the beg

2条回答
  •  失恋的感觉
    2021-01-21 07:29

    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:

    ; .rkt
    ; written in BSL
    (require "provide.rkt")
    
    (provide )
    
    (define ( ....) ....)
    

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

    ; written in BSL
    (require ".rkt")
    
    ( ....)
    

    For providing all definitions from a file

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

    ; .rkt
    ; written in BSL
    (require "provide.rkt")
    
    (provide (all-defined-out))
    
    (define ( ....) ....)
    
    (define ( ....) ....)
    
    ...
    

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

    ; written in BSL
    (require ".rkt")
    
    ( ....)
    
    ( ....)
    
    ...
    

提交回复
热议问题