How to pass a lambda expression in Elisp

2019-02-18 02:54发布

问题:

So, I'm trying to make a generic web search function in Elisp:

(defun look-up-base (url-formatter)
  (let (search url)
    (setq search(thing-at-point 'symbol))
    (setq url (url-formatter search))
    (browse-url url))
  )

This function will just grab the word under the cursor, format the word for web search using url-formatter and then open up the search string in the web browser to perform the search.

Next, I try to implement a function which will Google the word under the cursor, using the previous function as a basis.

(defun google ()
  (interactive)
  (look-up-base (lambda (search) (concat "http://www.google.com/search?q=" search))))

Emacs will not complain if I try to evaluate it, but when I try to use it, Emacs gives me this error message:

setq: Symbol's function definition is void: url-formatter

And I have no clue why this happens. I can't see anything wrong with the function, what am I doing wrong?

回答1:

I think you need to use funcall:

Instead of (url-formatter search) you should have (funcall url-formatter search).

Lisp expects the name of a function as the first element of a list. If instead you have a symbol associated with a lambda expression or function name, you need to use funcall.