How can I modify function bindings in Common Lisp?

前端 未结 2 1900
时光说笑
时光说笑 2020-12-12 01:13

Here is something you can do in Scheme:

> (define (sum lst acc)
    (if (null? lst)
        acc
        (sum (cdr lst) (+ acc (car lst)))))
> (define s         


        
2条回答
  •  有刺的猬
    2020-12-12 01:57

    Because Common Lisp has a different namespace for functions, you need to use symbol-function or fdefinition.

    CL-USER> (defun foo (a)
               (+ 2 a))
    FOO
    CL-USER> (defun debug-foo (a)
               (format t " DEBUGGING FOO: ~a" a)
               (+ 2 a))
    DEBUG-FOO
    CL-USER> (defun debug-foo-again (a)
               (format t " DEBUGGING ANOTHER FOO: ~a" a)
               (+ 2 a))
    DEBUG-FOO-AGAIN
    CL-USER> (foo 4)
    6
    CL-USER> (setf (symbol-function 'foo) #'debug-foo)
    #
    CL-USER> (foo 4)
     DEBUGGING FOO: 4
    6
    CL-USER> (setf (fdefinition 'foo) #'debug-foo-again)
    #
    CL-USER> (foo 4)
     DEBUGGING ANOTHER FOO: 4
    6
    CL-USER>
    

提交回复
热议问题