yuu_nkjm blog
2015-02-25(Wed) [長年日記]
[Kawa][Scheme] 文字列をReadしてEval
evalのスコープ
(define x "global") (let ((x "local")) (eval 'x (interaction-environment))) ;; => global ;; (eval x)となる.xはレキシカルスコープの影響を受けない (let ((x "local")) (eval x (interaction-environment))) ;; => local ;; 引数が先に評価され,(eval "local")となる. (eval (+ 1 2)) ;; evalの前に(+ 1 2)が評価される.つまり(eval 3) (eval '(+ 1 2)) ;; evalで(+ 1 2)が評価される
文字列をS式として読み込んで評価
(define p (open-input-string "(eval (+ 1 2))")) (eval (read p)) ;; => 3 (define p (open-input-string " ;; http://www.geocities.jp/m_hiroi/func/abcscm10.html (define (make-random seed) (define (irand) (set! seed (modulo (+ (* 69069 seed) 1) #x100000000)) seed) (define (random) (* (/ 1.0 #x100000000) (irand))) (lambda (x) (cond ((eq? x 'irand) (irand)) ((eq? x 'random) (random)) (else #f)))) ")) (eval (read p)) (define prg (make-random 1)) (prg 'irand) (prg 'irand) (prg 'random) (prg 'random)