Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

elisp - How to Create a Temporary Function in Emacs Lisp

I'm making some tedious calls to a bunch of functions, but the parameters will be determined at runtime. I wrote a simple function to keep my code DRY but giving it a name is unnecessary. I don't use this function anywhere else.

I'm trying to do it the way I would in Scheme, but I get a void-function error:

(let ((do-work (lambda (x y z)
                  (do-x x)
                  (do-y y)
                  ;; etc
                  )))
  (cond (test-1 (do-work 'a 'b 'c))
        (test-2 (do-work 'i 'j 'k))))

I could stick it all into an apply (e.g., (apply (lambda ...) (cond ...))) but that isn't very readable. Is there a better way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Like other lisps (but not Scheme), Emacs Lisp has separate namespaces for variables and functions (i.e. it is a ‘Lisp2’, not a ‘Lisp1; see Technical Issues of Separation in Function Cells and Value Cells for the origin and meaning of these terms).

You will need to use funcall or apply to call a lambda (or other function) that is stored in a variable.

(cond (test-1 (funcall do-work 'a 'b 'c))
      (test-2 (funcall do-work 'i 'j 'k))

Use funcall if you will always send the same number of arguments. Use apply if you need to be able to send a variable number of arguments.

The internal mechanism is that each symbol has multiple “cells”. Which cell is used depends on where the symbol is in an evaluated form. When a symbol is the first element of an evaluated form, its “function” cell is used. In any other position, its “value” cell is used. In your code, do-work has the function in its value cell. To access it as a function you use funcall or apply. If it were in the function cell, you could call it directly by using its name as the car of an evaluated form. You can accomplish this with flet or labels from the cl package.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...