[英]In Racket, if an unquoted pair is constructed with the dot notation, is it possible to use a variable or an expression value for the second element?
In Racket, the following works: 在Racket中,以下工作:
(+ . [1 2]) ; => 3
{ define a + }
(a . [1 2]) ; => 3
However, i see no way to define b
to be the (1 2)
list so as to get (+ . b)
and (a . b)
to return 3
. 但是,我认为没有办法将
b
定义为(1 2)
列表,以便得到(+ . b)
和(a . b)
返回3
。 Is it possible? 可能吗?
Sure, just use apply
: 当然,只需使用
apply
:
(define a +)
(define b '(1 2))
(apply a b) ; => 3
(apply + b) ; => 3
How about this ... without using apply
but using eval
. 怎么样...不使用
apply
但使用eval
。 But seriously, using apply
is a better idea in this case, there's nothing wrong with it ( eval
is evil though, see the documentation to understand the last part with the namespace): 但严重的是,在这种情况下使用
apply
是一个更好的主意,它没有任何问题(虽然eval
是邪恶的 ,请参阅文档以了解命名空间的最后部分):
(define a +)
(define b '(1 2))
; executing in the evaluation window
(eval `(+ ,@b))
=> 3
(eval `(a ,@b))
=> 3
; executing from the definitions window
(define-namespace-anchor an)
(define ns (namespace-anchor->namespace an))
(eval `(+ ,@b) ns)
=> 3
(eval `(a ,@b) ns)
=> 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.