Ele*_*fee 3 clojure common-lisp lisp-2
在Clojure中,我可以定义一系列函数,然后将它们称为任何其他值,如下所示:
(doseq [op [+ - * /]]
  (println (op 1 2 3 4)))
产生以下输出:
10
-8
24
1/24
nil
尝试在Common Lisp中执行相同操作只会导致错误:
(dolist (op '(+ - * /))
  (print (op 1 2 3 4))
; in: DOLIST (OP '(+ - * /))
;     (LET ((OP (TRULY-THE (MEMBER / * - +) (CAR #:N-LIST671))))
;       (SETQ #:N-LIST671 (CDR #:N-LIST671))
;       (TAGBODY (PRINT (OP 1 2 3 4))))
; 
; caught STYLE-WARNING:
;   The variable OP is defined but never used.
; in: DOLIST (OP '(+ - * /))
;     (OP 1 2 3 4)
; 
; caught STYLE-WARNING:
;   undefined function: OP
; 
; compilation unit finished
;   Undefined function:
;     OP
;   caught 2 STYLE-WARNING conditions
debugger invoked on a UNDEFINED-FUNCTION:
  The function COMMON-LISP-USER::OP is undefined.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.
("undefined function")
调用op作为#'op没有为我工作,要么.
那么有没有办法在CL中做到这一点?
由于函数的命名空间和数据的命名空间在LISP-2中是分开的(因此在common-lisp中),因此在funcall将函数作为参数传递时需要使用:
(dolist (op '(+ - * /)) 
   (print (funcall op 1 2 3 4))))
funcall函数大致相当于Clojure apply,并将op函数应用于提供的参数.在这种情况下,还有一件事情要发生,因为它op是一个绑定到函数的符号,但funcall会处理它.