用大括号 {} 替换 Racket 中的“开始" [英] Curly brackets {} to replace 'begin' in Racket

查看:72
本文介绍了用大括号 {} 替换 Racket 中的“开始"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有宏可以用大括号{}来表示语句块,从而代替'begin'关键字.因此,而不是:

Is it possible to have a macro to use curly brackets {} to indicate a block of statments, so as to replace the 'begin' keyword. Hence, instead of:

(if (condition)
    (begin 
        (statement1)
        (statement2)
        (statement3)
        (statement4))
    (else-statement))

我们可以使用:

(if (condition) {
        (statement1)
        (statement2)
        (statement3)
        (statement4) }
    (else-statement))

如何实现?感谢您的回答.

How can this be accomplished? Thanks for your answers.

推荐答案

这是完全可能的,并且有几种方法可以做到.(开始之前的快速说明,我将使用 block 而不是 begin 因为它在使用内部定义时表现更好.)

This is completely possible, and there are several ways to do it. (Quick note before I start, I'm going to use block instead of begin because it behaves better with internal definitions.)

一种稍微有点老套的方法是重新定义函数应用程序的含义,以便对花括号进行特殊处理.您可以通过定义一个 #%app 宏来实现:

One slightly hack-y way is to redefine what function application means so that curly-braces are treated specially. You can do this by defining an #%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

方法二:扩展阅读器

另一种方法是定义一种新的 #lang 语言并使用 { 字符的不同条目扩展可读表.让我去做那个......

Method 2: Extending the Reader

Another way would be to define a new #lang language and extend the readtable with a different entry for the { character. Let me go and do that ...

要定义#lang 语言,您需要将阅读器实现放在your-language/lang/reader.rkt 中.这里是 curly-block/lang/reader.rkt,其中 curly-block 目录作为单集合包安装(raco pkg install path/to/curly-block).

To define a #lang language, you need to put the reader implementation in your-language/lang/reader.rkt. Here that's curly-block/lang/reader.rkt, where the curly-block directory is installed as a single-collection package (raco pkg install path/to/curly-block).

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

这篇关于用大括号 {} 替换 Racket 中的“开始"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆