all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: defun vs lambda
Date: Fri, 24 Jul 2009 23:46:01 +0200	[thread overview]
Message-ID: <87d47pkb52.fsf@galatea.local> (raw)
In-Reply-To: b453056c-ef8e-47ba-aa45-1c90fef7233c@k1g2000yqf.googlegroups.com

weber <hugows@gmail.com> writes:

> Hi,
>
> I know I can return a function from another function, like this:
>
> (setq f (lambda () 2))
>
> but I have to call it like (funcall f). Is there anyway that I could
> do:
>
> (setf f (defun () 2) and then be able to call (f) ?

Notice that funcall accepts various kinds of "functions":
    - symbols naming functions,
    - lambda expressions representing anonymous functions,
    - byte code (compiled-functions),
    - primitives ("subr" functions),
    and possibly others (such as macros).




(defun plus (x y) (+ x y))
(defun compiled-plus (x y) (+ x y))
(byte-compile 'compiled-plus)

(let ((lambda-expression            (quote         (lambda (x y) (+ x y))))
      (anonymous-function           (function      (lambda (x y) (+ x y))))
      (compiled-anonymous-function  (byte-compile '(lambda (x y) (+ x y))))
      (function-name                (quote    plus))
      (named-function               (function plus))
      (compiled-named-function      (function compiled-plus))
      (interpreted-function         (symbol-function 'plus))
      (compiled-function            (symbol-function 'compiled-plus))
      (primitive-function           (symbol-function '+)))
   (dolist (name  '(lambda-expression anonymous-function
                    compiled-anonymous-function function-name named-function
                    compiled-named-function interpreted-function
                    compiled-function primitive-function))
     (insert (format "%s\n\t--> %S\n\t==: %S\n\t==> %S\n\n"
                     name (symbol-value name) (type-of (symbol-value name))
                     (funcall (symbol-value name) 32 10)))))

lambda-expression
	--> (lambda (x y) (+ x y))
	==: cons
	==> 42

anonymous-function
	--> (lambda (x y) (+ x y))
	==: cons
	==> 42

compiled-anonymous-function
	--> #[(x y) "\b	\\\207" [x y] 2]
	==: compiled-function
	==> 42

function-name
	--> plus
	==: symbol
	==> 42

named-function
	--> plus
	==: symbol
	==> 42

compiled-named-function
	--> compiled-plus
	==: symbol
	==> 42

interpreted-function
	--> (lambda (x y) (+ x y))
	==: cons
	==> 42

compiled-function
	--> #[(x y) "\b	\\\207" [x y] 2]
	==: compiled-function
	==> 42

primitive-function
	--> #<subr +>
	==: subr
	==> 42

nil

The difference between quote and function, in emacs lisp, can be
detected only when you compile the code that use them.  function let
the compiler know that its argument is actually code, so it will
compile it.  quote let the compiler know that its argument is actually
data, so it won't compile it: the lambda expression returned by quote
will always be considered as data (and happily interpreted by emacs
lisp).


> (setf f (defun () 2) and then be able to call (f) ?

This defun form is invalid. You must give a function name:

   (setf f (defun g () 2))

defun returns the name of the function, that is the symbol g, and f
will be bound to that symbol.  So when you (funcall f), you will call
the function named by g.  But you still have to write (funcall f).

You can of course name the function f, and so be able to directly call
it:

    (defun f () 2)
    (f) --> 2



Now to answer your formal question, there is no way to call the
function bound to a variable without using funcall (or apply).  This
is because emacs lisp is what is called, a "Lisp-2".
Have a look at: http://www.nhplace.com/kent/Papers/Technical-Issues.html

When we define a function with defun, what happens, is that the value
binding of the name of the function is not changed:

    (setf f 42)
    (defun f () 2)
    f --> 42

but instead, a function slot is changed: the symbol is "fbound".

    (unintern 'f)                     ; clean up
    (list (boundp 'f) (fboundp 'f))   ; --> (nil nil)
    (setf f 42)                       ; --> 42
    (list (boundp 'f) (fboundp 'f))   ; --> (t nil)
    (defun f () 2)                    ; --> f
    (list (boundp 'f) (fboundp 'f))   ; --> (t t)

In the case of emacs lisp, which proposes only special variables, when
the symbol is bound, you can  find the binding of a variable  in the
value slot of the symbol:
    
    (symbol-value 'f)                 ; --> 42

Similarly, when the symbol is fbound, you can find the function
binding of the function int he function slot of the symbol:

    (symbol-function 'f)              ; --> (lambda nil 2)

These operators are accessors, so you could modify the function slot
as well as the value slot:


   (setf (symbol-value 'f) 42)                   ; <=> (setf f 42)
   (setf (symbol-function 'f) (lambda () 2))     ; <=> (defun f () 2)

So in a way, you could write that:

   (setf (symbol-function 'f) (lambda () 2))

and then be able to call the function f directly:

   (f) ; --> 2


But it's simplier to write (defun f () 2).
Unless of course you have to generate the body of the function at run-time.

  (setf (symbol-function 'f) (compute-some-lambda-expression))
  (f) ; --> ?
  (byte-compile 'f)
  (f) ; --> ? faster.

or just:

  (setf (symbol-function 'f) (byte-compile (compute-some-lambda-expression)))
  (f) ; --> ? faster.

but you didn't mention generating code at run-time, so I guess I
overextended myself.  Sorry.

-- 
__Pascal Bourguignon__


  parent reply	other threads:[~2009-07-24 21:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-24 20:30 defun vs lambda weber
2009-07-24 21:23 ` Anselm Helbig
2009-07-24 21:46 ` Pascal J. Bourguignon [this message]
2009-07-31 11:41   ` weber
2009-07-31 12:56     ` Pascal J. Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d47pkb52.fsf@galatea.local \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.