はじめてのScheme

記念に。読むなよはずかしい。

値渡しであることが信じられなかったので処理系インストール。

値渡しだなぁ。

 

> "HelloWorld"
"HelloWorld"

> '()
'()

> '("HelloWorld")
'("HelloWorld")

> (define vhello "HelloWorld")
> vhello
"HelloWorld"

> (display vhello)
HelloWorld

> (define fhello ( lambda () "HelloWorld"))
> fhello
#<procedure:fhello>

> (fhello)
"HelloWorld"

> (define sayit (lambda () vhello))
> (sayit)
"HelloWorld"

> (define sayit2 (lambda (s) s))
> (sayit2 vhello)
"HelloWorld"

> (define sayit4 (lambda (s)(set! s "GoodByWorld!") s))
> (sayit4 vhello)
"GoodByWorld!"
> vhello
"HelloWorld"

> (define displayit(lambda (s) (display s)))
> (define sayit5(lambda (s l) (l s)))
> (sayit5 vhello displayit)
HelloWorld

> (define displayit2(lambda (s) (display (string-append s "!"))))
> (sayit5 vhello displayit2)
HelloWorld!

> (define sayit6(lambda (s l) (set! l displayit2)(l s)))
> (sayit6 vhello displayit)
HelloWorld!
> (sayit6 vhello displayit2)
HelloWorld!
> (sayit5 vhello displayit)
HelloWorld

???

お前大域変数変更できないの?…タバコ一本吸って気づいた。

 

> (define changeworld(lambda()(set! vhello "GoodByWorld!") vhello))
> (changeworld)
"GoodByWorld!"
> vhello
"GoodByWorld!"

 

そりゃそうだ。