10

I am reading SICP recently to learn lisp programming.

There is an example in the book to make a high level function sum as below.

 (define (sum term a next b)
   ...
   (term a)
   ...
   (next a)
   ...

In this function, term and next are functions.

I want to write the same function in elisp like this.

 (defun sum (term a next b)
    ....
    (funcall term a)
    ...
    (func next a)
    ....

Then I define some test functions to test sum

 (defun inc-test (x) (+ x 1))

 sum inc-test 1 inc-test 10 

but it show errors like this: Invalid function: inc-test

As I am new to elisp, can anyone call tell me how to fix it? very appreciate

7
  • How are you calling sum?
    – Dan
    Commented Aug 14, 2015 at 10:49
  • Hi, add the calling example
    – Luis404
    Commented Aug 14, 2015 at 10:52
  • 3
    Emacs Lisp has two namesapces for functions and for variables. This means that inc-test in your last example is used as a variable, but is not defined as a variable (it is a function). You need to pass either a symbol or a function reference to funcall. Hence one way of fixing your code is to pass it 'inc-test instead of inc-test (note the quote).
    – wvxvw
    Commented Aug 14, 2015 at 10:58
  • do you mean this: sum 'inc-test 1 'inc-test 10 ??? got this error: usage: sum : (TERM A NEXT B)
    – Luis404
    Commented Aug 14, 2015 at 11:00
  • 3
    The two namespaces vs one issue is one of the big differences between elisp and common lisp on one hand, and scheme on the other. You are bound to be confused if you read SICP and practice in a lisp-2 (as the kind with separate name spaces for variables and functions are called). Perhaps you should get an actual scheme implementation to play with? Commented Aug 14, 2015 at 13:39

1 Answer 1

9

To recap what is in the comments, funcall unquotes its argument (by just accessing the argument). And before that, we also need to make sure that we don't try to evaluate or access the value of the function name parameter that is passed in the call to the function.

Therefore, an acceptable answer seems to be:

(defun sum (term a next b)
    (funcall term a)
    (funcall next a))

(defun inc-test (x) (+ x 1))

(sum 'inc-test 1 'inc-test 10 )

Or basically call with 'inc-test or (quote inctest) rather than inc-test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.