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?

≯℡__Kan透↙ 提交于 2019-12-02 08:53:23

问题


In Racket, the following works:

(+ . [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. Is it possible?


回答1:


Sure, just use apply:

(define a +)
(define b '(1 2))
(apply a b)       ; => 3
(apply + b)       ; => 3



回答2:


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


来源:https://stackoverflow.com/questions/15631001/in-racket-if-an-unquoted-pair-is-constructed-with-the-dot-notation-is-it-possi

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