niitsuma blog このページをアンテナに追加 RSSフィード

2008-11-13 [lisp]scheme

[] racket module nest and global variable

Let us consider the case global variable my-global-var is exported in module sub-sub.scm

sub-sub.scm
#lang racket
(provide  my-global-var print-my-global-var set-my-global-var!)
(define my-global-var "global in sub-sub")
(define (print-my-global-var) (display my-global-var) (newline))
(define (set-my-global-var! v) (set! my-global-var v))

We can refer the global variable my-global-var from other module sub.scm

sub.scm
#lang racket
(provide set-my-global-var-in-sub print-my-global-var-in-sub)
(require "sub-sub.scm")
(define (set-my-global-var-in-sub) (set-my-global-var! "in sub"))
(define (print-my-global-var-in-sub)

Also we can refer the global variable the my-global-var from sub.scm (nest refer) .

Again we can refer the global variable the my-global-var from sub-sub.scm directly .

main.scm
#lang racket

(require "sub.scm") ;nest refer
(require "sub-sub.scm") ;direct refer

(display  my-global-var)(newline)
(print-my-global-var-in-sub)
(print-my-global-var)

(set-my-global-var! "call sub sub")

(display  my-global-var)(newline)
(print-my-global-var-in-sub)
(print-my-global-var)

(set-my-global-var-in-sub)

The nested refer of the global variable the my-global-var from sub.scm and from sub-sub.scm is same?

The following result shows they are same.

$racket main.scm

global in sub-sub
global in sub-sub
global in sub-sub
call sub sub
call sub sub
call sub sub
in sub
in sub
in sub

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

トラックバック - http://d.hatena.ne.jp/niitsuma/20081113/1324207362