| 1 |
#!r6rs |
| 2 |
;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named |
| 3 |
;; LICENSE from the original collection this file is distributed with. |
| 4 |
|
| 5 |
;; Fall-back library in case the host Scheme system does not provide SRFI-39. |
| 6 |
|
| 7 |
(library (srfi :39 parameters) |
| 8 |
(export |
| 9 |
make-parameter |
| 10 |
parameterize) |
| 11 |
(import |
| 12 |
(rnrs)) |
| 13 |
|
| 14 |
(define make-parameter |
| 15 |
(case-lambda |
| 16 |
((val) (make-parameter val values)) |
| 17 |
((val guard) |
| 18 |
(unless (procedure? guard) |
| 19 |
(assertion-violation 'make-parameter "not a procedure" guard)) |
| 20 |
(let ((p (case-lambda |
| 21 |
(() val) |
| 22 |
((x) (set! val (guard x)))))) |
| 23 |
(p val) |
| 24 |
p)))) |
| 25 |
|
| 26 |
(define-syntax parameterize |
| 27 |
;; Derived from Ikarus's implementation of parameterize. |
| 28 |
(lambda (stx) |
| 29 |
(syntax-case stx () |
| 30 |
((_ () b0 b ...) |
| 31 |
#'(let () b0 b ...)) |
| 32 |
((_ ((p e) ...) b0 b ...) |
| 33 |
(with-syntax (((tp ...) (generate-temporaries #'(p ...))) |
| 34 |
((te ...) (generate-temporaries #'(e ...)))) |
| 35 |
#'(let ((tp p) ... |
| 36 |
(te e) ...) |
| 37 |
(let ((swap (lambda () |
| 38 |
(let ((t (tp))) |
| 39 |
(tp te) |
| 40 |
(set! te t)) |
| 41 |
...))) |
| 42 |
(dynamic-wind |
| 43 |
swap |
| 44 |
(lambda () b0 b ...) |
| 45 |
swap)))))))) |
| 46 |
|
| 47 |
) |
| 48 |
|