0

I mimic a general map function from SICP's

(define (map proc items)
  (if (null? items)
      nil
      (cons (proc (car items))
            (map proc (cdr items)))))
(map abs (list -10 2.5 -11.6 17))

Rephrase it with elisp

(defun map(proc items)
  (if (null items)
      nil
      (cons (proc (car items))
            (map proc (cdr items)))))
(map abs (list -10 2.5 -11.6 17))

Run but report error:

ELISP> (map abs (list -3 -5))
*** Eval error ***  Symbol’s value as variable is void: abs

However, abs works

ELISP> (map abs (list -3 -5))
*** Eval error ***  Symbol’s value as variable is void: abs

What's the problem with my rewriting?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
  • 1
    Possible duplicate of [How to pass a lambda expression in Elisp](https://stackoverflow.com/questions/5357656/how-to-pass-a-lambda-expression-in-elisp) – Steve Vinoski Oct 30 '19 at 11:50
  • (1) The symbol's value as variable is void, which means when symbol `abs` is being used in an evaluation context where it is expected to be a variable; but it is not bound in the variable namespace. It is bound, however, in the function namespace (Emacs-Lisp is a Lisp-2). To access the *function* named `abs` you write `(function abs)`; or, you can refer to the function by its name, and in that case it will be the underlying call to funcall that will resolve the name to the function. (2) you should `(funcall proc (car items))` instead of `(proc ...)`, or else it calls *literally* `proc`. – coredump Oct 30 '19 at 14:30

0 Answers0