What's the difference between ' and #' in Lisp?

后端 未结 3 644
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 07:39

It seems both

(mapcar \'car \'((foo bar) (foo1 bar1))) 

and

(mapcar #\'car \'((foo bar) (foo1 bar1)))

w

相关标签:
3条回答
  • 2020-12-31 08:02

    Why these 2 both work in previous mapcar?

    The documentation for mapcar says:

    If function is a symbol, it is coerced to a function as if by symbol-function.

    0 讨论(0)
  • 2020-12-31 08:04
    'foo
    

    evaluates to the symbol FOO.

    #'foo
    

    evaluates to the function bound to the name FOO.

    In Lisp a symbol can be called as a function when the symbol FOO has a function binding. Here CAR is a symbol that has a function binding.

    But this does not work:

    (flet ((foo (a) (+ a 42)))
      (mapcar 'foo '(1 2 3 4 5)))
    

    That's because FOO as a symbol does not access the local lexical function and the Lisp system will complain when foo is not a function defined elsewhere.

    We need to write:

    (flet ((foo (a) (+ a 42)))
      (mapcar #'foo '(1 2 3 4 5)))
    

    Here the (function foo) or its shorthand notation #'foo refers to the lexical local function FOO.

    Note also that in

    (funcall #'foo ...)
    

    vs.

    (funcall 'foo ...)
    

    The later might do one more indirection, since it needs to lookup the function from the symbol, while #'foo denotes the function directly.

    Summary:

    If a symbol has a function binding, calling a function through the symbol works.

    0 讨论(0)
  • 2020-12-31 08:12

    Try passing an anonymous function (lambda) to your mapcar and you'll see that #' is required since the quote by itself expects a symbol that is bound to a function, but the symbol doesn't exist in an un-named function:

    CL-USER> (mapcar '(lambda (x) (format t "it is ~d" x)) (list 3 5 7))
    ; Evaluation aborted on #<TYPE-ERROR expected-type: (OR FUNCTION SYMBOL)
                 datum: (LAMBDA (X) (FORMAT T "it is ~d" X))>.
    

    vs:

    CL-USER> (mapcar #'(lambda (x) (format t "it is ~d" x)) (list 3 5 7))
    it is 3it is 5it is 7
    (NIL NIL NIL)
    
    0 讨论(0)
提交回复
热议问题