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?

时间秒杀一切 提交于 2019-12-02 03:26:22

Sure, just use apply:

(define a +)
(define b '(1 2))
(apply a b)       ; => 3
(apply + b)       ; => 3
Óscar López

How about this ... without using apply but using 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):

(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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!