问题
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