这是完全可能的,并且有多种方法可以做到。 (在开始之前快速说明,我将使用block http://docs.racket-lang.org/reference/block.html代替begin
因为它在内部定义中表现得更好。)
方法一:重新定义#%app
一种稍微有点 hack 的方法是重新定义函数应用的含义,以便对大括号进行特殊处理。您可以通过定义一个来做到这一点#%app
macro:
#lang racket
(require racket/block syntax/parse/define (prefix-in - racket))
;; This #%app macro redefines what function application means so that
;; { def-or-expr ... } expands into (block def-or-expr ...)
;; Otherwise it uses normal function application
(define-syntax-parser #%app
[{_ def-or-expr:expr ...}
#:when (equal? #\{ (syntax-property this-syntax 'paren-shape))
;; group them in a block
#'(block def-or-expr ...)]
[(_ f:expr arg ...)
#:when (not (equal? #\{ (syntax-property this-syntax 'paren-shape)))
;; expand to the old #%app form, from (prefix-in - racket)
#'(-#%app f arg ...)])
;; using it:
(define (f x)
(if (< 5 x) {
(define y (- x 5))
(f y)
}
x))
(f 1) ; 1
(f 5) ; 5
(f 6) ; 1
(f 10) ; 5
(f 11) ; 1
方法2:扩展阅读器
另一种方法是定义一个新的#lang
语言并使用不同的条目扩展可读表{
特点。让我去这样做......
定义一个#lang
语言,您需要将阅读器实现放入your-language/lang/reader.rkt
。这就是curly-block/lang/reader.rkt
,其中curly-block
目录作为单个集合包安装(raco pkg install path/to/curly-block
).
卷曲块/lang/reader.rkt
;; s-exp syntax/module-reader is a language for defining new languages.
#lang s-exp syntax/module-reader
racket
#:wrapper1 (lambda (th)
(parameterize ([current-readtable (make-curly-block-readtable (current-readtable))])
(th)))
;; This extends the orig-readtable with an entry for `{` that translates
;; { def-or-expr ... } into (block def-or-expr ...)
(define (make-curly-block-readtable orig-readtable)
(make-readtable orig-readtable
#\{ 'terminating-macro curly-block-proc))
;; This is the function that the new readtable will use when in encounters a `{`
(define (curly-block-proc char in src ln col pos)
;; This reads the list of things ending with the character that closes `char`
;; The #f means it uses the racket reader for the first step, so that `{`
;; uses the normal behavior, grouping expressions into a reader-level list
(define lst (read-syntax/recursive src in char #f))
(cons 'block lst))
使用它:
#lang curly-block
(require racket/block)
(define (f x)
(if (< 5 x) {
(define y (- x 5))
(f y)
}
x))
(f 1) ; 1
(f 5) ; 5
(f 6) ; 1
(f 10) ; 5
(f 11) ; 1