3

Possible Duplicate:
How to pass a lambda expression in Elisp

I have following code:

(defun my-map (p l)
  (mapcar (lambda (el) (p el)) l))

(defun test ()
  (my-map (lambda (x) (+ x 1)) (list 1 2 3)))

(It's example - not actual code I tried to write). It complains that it cannot find function p:

Debugger entered--Lisp error: (void-function p)
  (p el)
  (lambda (el) (p el))(1)
  mapcar((lambda (el) (p el)) (1 2 3))
  my-map((lambda (x) (x + 1)) (1 2 3))
  test()
  eval((test) nil)
  eval-expression((test) nil)
  call-interactively(eval-expression nil nil)
  recursive-edit()
  debug(error (void-variable test))
  eval(test nil)
  eval-expression(test nil)
  call-interactively(eval-expression nil nil

I guess that it treats a p as symbol and not variable bounded in outer scope. How to make it work?

Community
  • 1
  • 1
Maciej Piechotka
  • 7,028
  • 6
  • 39
  • 61
  • possible duplicate of [How to pass a lambda expression in Elisp](http://stackoverflow.com/questions/5357656/how-to-pass-a-lambda-expression-in-elisp) and [How do I pass a function as a parameter to in elisp?](http://stackoverflow.com/questions/213267/how-do-i-pass-a-function-as-a-parameter-to-in-elisp) – phils May 25 '12 at 06:16
  • @phils: You're right (I voted to close - strange I assumed that author can close it's own question as duplicate). – Maciej Piechotka May 25 '12 at 06:22
  • This might also be useful: http://stackoverflow.com/questions/9942675/in-elisp-how-do-i-put-a-function-in-a-variable/9943606#9943606 – phils May 25 '12 at 06:23

1 Answers1

10

You want to use funcall.

funcall is a built-in function in `C source code'.

(funcall FUNCTION &rest ARGUMENTS)

Call first argument as a function, passing remaining arguments to it. Return the value that function returns. Thus, (funcall 'cons 'x 'y) returns (x . y).