forked from OnRoadZy/RacketGuideInChinese
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06.1 模块基础知识
More file actions
35 lines (21 loc) · 1.13 KB
/
Copy path06.1 模块基础知识
File metadata and controls
35 lines (21 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
6.1 模块基础知识
每个Racket模块通常驻留在自己的文件中。例如,假设文件“cake.rkt”包含以下模块:
"cake.rkt"
#lang racket
(provide print-cake)
; draws a cake with n candles
(define (print-cake n)
(show " ~a " n #\.)
(show " .-~a-. " n #\|)
(show " | ~a | " n #\space)
(show "---~a---" n #\-))
(define (show fmt n ch)
(printf fmt (make-string n ch))
(newline))
然后,其他模块可以导入“cake.rkt”以使用print-cake的函数,因为“cake.rkt”的provide行明确导出了print-cake的定义。show函数对"cake.rkt"是私有的(即它不能从其他模块被使用),因为show没有被导出。
下面的“random-cake.rkt”模块导入“cake.rkt”:
"random-cake.rkt"
#lang racket
(require "cake.rkt")
(print-cake (random 30))
相对在导入(require "cake.rkt")内的引用“cake.rkt”的运行来说,如果“cake.rkt”和“random-cake.rkt”模块在同一个目录里。UNIX样式的相对路径用于所有平台上的相对模块引用,就像HTML页面中的相对的URL一样。