与基本函数同名的球拍宏函数
Racket macro function with same name as basic functions
是否可以定义一个与球拍中的函数同名的宏,但不覆盖它。 (因此该名称将有 2 种可能的用法,具体取决于传递的参数)
例如函数round.
1.用法(球拍功能)
( round 1.2 )
-> 1
2。用法(我们的宏)
(round (someStruct arg))
-> resultStruct
使用函数
(require (only-in racket [round @round]))
(define (my-round v)
;; do whatever you want to do here
"hello")
(define (round v)
(cond
[(number? v) (@round v)]
[else (my-round v)]))
(define x 1.2)
(round x) ;=> 1.0
(define y "abc")
(round y) ;=> "hello"
通过将round
定义为一个函数,对在运行时[=33]传入函数的值进行案例分析=].
使用宏
(require syntax/parse/define
(only-in racket [round @round]))
(define-syntax-parser round
[(_ x:number) #'(@round x)]
[(_ x) #'"hello"])
(define x 1.2)
(round x) ;=> "hello"
(define y "abc")
(round y) ;=> "hello"
(round 1.2) ;=> 1.0
(round (this is not a sensible expression but it works (()))) ;=> "hello"
通过将 round
定义为宏,案例分析是在 编译时传递给宏的 语法片段 上完成的。在上面的示例中,当宏的操作数是文字数字时,我们将使用实际的 Racket 的 round
。其他所有内容都将转换为 "hello"
。请注意,在编译时,像 x
这样的标识符还没有被评估和关联到一个值。你唯一知道的是它是一个标识符,它不是文字数字,所以它转换为"hello"
。这个宏展开后的程序大概是:
(require syntax/parse/define
(only-in racket [round @round]))
(define x 1.2)
"hello"
(define y "abc")
"hello"
(@round 1.2)
"hello"
选择您喜欢的一个。我怀疑你真的想使用函数而不是宏。
是否可以定义一个与球拍中的函数同名的宏,但不覆盖它。 (因此该名称将有 2 种可能的用法,具体取决于传递的参数)
例如函数round.
1.用法(球拍功能)
( round 1.2 )
-> 1
2。用法(我们的宏)
(round (someStruct arg))
-> resultStruct
使用函数
(require (only-in racket [round @round]))
(define (my-round v)
;; do whatever you want to do here
"hello")
(define (round v)
(cond
[(number? v) (@round v)]
[else (my-round v)]))
(define x 1.2)
(round x) ;=> 1.0
(define y "abc")
(round y) ;=> "hello"
通过将round
定义为一个函数,对在运行时[=33]传入函数的值进行案例分析=].
使用宏
(require syntax/parse/define
(only-in racket [round @round]))
(define-syntax-parser round
[(_ x:number) #'(@round x)]
[(_ x) #'"hello"])
(define x 1.2)
(round x) ;=> "hello"
(define y "abc")
(round y) ;=> "hello"
(round 1.2) ;=> 1.0
(round (this is not a sensible expression but it works (()))) ;=> "hello"
通过将 round
定义为宏,案例分析是在 编译时传递给宏的 语法片段 上完成的。在上面的示例中,当宏的操作数是文字数字时,我们将使用实际的 Racket 的 round
。其他所有内容都将转换为 "hello"
。请注意,在编译时,像 x
这样的标识符还没有被评估和关联到一个值。你唯一知道的是它是一个标识符,它不是文字数字,所以它转换为"hello"
。这个宏展开后的程序大概是:
(require syntax/parse/define
(only-in racket [round @round]))
(define x 1.2)
"hello"
(define y "abc")
"hello"
(@round 1.2)
"hello"
选择您喜欢的一个。我怀疑你真的想使用函数而不是宏。