0

I've got a problem when i want to execute the following code :

    (defun sum1
      (lambda (n)
        (+ n 1)))

When i running with M-x ielm appears the next message :

Invalid function: (lambda (lambda (n) (+ n 1)) nil)

I would like to make it clear that while I might use the following code :

    (defun sum1(n)
      (+ n 1))

I would like to know how I define a lambda in this case.

All information is accepted, I am newbie.

5
  • 3
    you can macroexpand defun and see that it just doing (defalias 'sum1 (lambda (n) (+ n 1))) Commented Mar 13, 2017 at 22:58
  • 3
    Lambda is for an anonymous function. It's not really clear what you're asking. You could have, e.g,. (defun make-adder (n) (lambda (x) (+ x n))) that returns a function that adds n to a number. You could have an anonymous function as the value of a variable: (let ((adder (lambda (x) (+ x 2)))) ...). Commented Mar 13, 2017 at 22:58
  • I tried to write a newbie-suitable answer for this but I realise that the real answer is: if you want to learn lisp don't start with elisp. Instead start with either Common Lisp (a traditional but much more sorted-out lisp than elisp), Scheme (a more modern lisp) or perhaps something else (Clojure?). There is nothing wrong with elisp, but it is really old-fashioned: it's fine for what it does, but it's not how to learn Lisp as it will just confuse you. (If what you want to know how to do is hack emacs, then elisp is a good place to start of course.) Commented Mar 14, 2017 at 15:33
  • Finally i made it , (eval '(defun sum1 () (function (lambda (n) (+ n 1))) t)¨ (funcall (sum1) 5) ; 6 Commented Jul 14, 2017 at 4:29
  • @Rovaceni23 Just in case it is still relevant: Your use of eval here should have no effect other than preventing useful compiler warnings and wrapping lambda in function should be entirely redundant. Commented Aug 3, 2017 at 13:43

2 Answers 2

3

lambda makes a function, but doesn't give it a name. Use it whenever you need to create a function, but don't need to give it a name. A good example is a comparison function that's only used once to sort a list.

(let ((l (number-sequence 1 10)))
  (sort l (lambda (a b) (> a b)))) ;; reversed

It could be annoying to defun a function that you're never going to use again. It takes a small bit of effort to come up with a name, and you need to make sure that you're not overwriting an existing function (which isn't really hard, I usually prefix my functions with my initials).

An example of a common abuse of lambda (IMO) is for hooks. Many people will add lambdas to hooks, but if you ever look at the value of the hook variable, you have a bunch of lisp instead of a function name that you can run describe-function on. But far worse, you can't use remove-hook, or redefine the function while you're writing it or debugging it.

Sign up to request clarification or add additional context in comments.

Comments

2

defun is used to declare a function.

In scheme you used to declare a variable and assigned it a variable expression like this: (define x variable-expression), and when declaring a function you used to write something like this (define x function-expression). Ofc variable-expression could be another variable previously declared or a literal (i.e define x 5, define x y).

The same for a function expression, but when writing a function 'literal' you do it using an anonymous function like this:

(define x (lambda (x) (....)))

where lambda (x) (...) is a function expression that actually returns a function and is assigned to x. So this way x is declared as a function.

defun actually declares a function, not a variable as define does in scheme. So when you write:

(defun sum1
 (lambda (n)
   (+ n 1)))

What really happens is that sum1 is declared as a function with no arguments that returns another function that actually accepts a variable n and return n+1.

What you really want is sum1 to be declared as a function instead of a function that returns another function, which is what you actually get by using defun and lambda in the same snippet.

EDIT1: you can use your function like this ((sum1) 5)

2 Comments

Actually you need (defun sum1 () (lambda ...)): you need the arglist. Also without lexical scope (which elisp does not have by default) this definition does not really work at all.
it was a long explanation, but not the clearest. Please tell the guy how to solve his problem\ write his code correctly, as @tfb did above.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.