-
Notifications
You must be signed in to change notification settings - Fork 4
/
parameter.ss
34 lines (29 loc) · 1.08 KB
/
parameter.ss
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
#lang scheme/base
(require (for-syntax scheme/base)
"base.ss")
; (any -> boolean) string -> (any -> any)
;
; Makes a procedure that takes a single argument as a parameter and
; checks it against a predicate. If it matches, it returns the value.
; If not, it throws an exception. Useful as a guard procedure for a
; parameter.
(define (make-guard pred type-message)
(lambda (val)
(if (pred val)
val
(raise-exn exn:fail:contract
(format "Expected ~a, received ~s" type-message val)))))
; syntax (define-parameter identifier any (any -> any) identifier)
(define-syntax (define-parameter stx)
(syntax-case stx ()
[(_ id initial-value guard with-form)
#'(begin (define id
(make-parameter initial-value guard))
(define-syntax (with-form stx)
(syntax-case stx ()
[(with-form new-value exp (... ...))
#'(parameterize ([id new-value])
exp (... ...))])))]))
; Provide statements -----------------------------
(provide make-guard
define-parameter)