如何在 Elisp 中传递 lambda 表达式 [英] How to pass a lambda expression in Elisp

查看:27
本文介绍了如何在 Elisp 中传递 lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试在 Elisp 中创建一个通用的网络搜索功能:

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))
  )

这个函数只会抓取光标下的单词,使用 url-formatter 格式化该单词进行网络搜索,然后在网络浏览器中打开搜索字符串进行搜索.

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 不会抱怨,但是当我尝试使用它时,Emacs 给了我这个错误信息:

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?

推荐答案

我认为你需要使用 funcall:

你应该使用 (funcall url-formatter search) 而不是 (url-formatter search).

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

Lisp 期望函数名作为列表的第一个元素.如果您有一个与 lambda 表达式或函数名称相关联的符号,则需要使用 funcall.

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.

这篇关于如何在 Elisp 中传递 lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆