all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* defun vs lambda
@ 2009-07-24 20:30 weber
  2009-07-24 21:23 ` Anselm Helbig
  2009-07-24 21:46 ` Pascal J. Bourguignon
  0 siblings, 2 replies; 5+ messages in thread
From: weber @ 2009-07-24 20:30 UTC (permalink / raw)
  To: help-gnu-emacs

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) ?

Thanks
weber


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: defun vs lambda
  2009-07-24 20:30 defun vs lambda weber
@ 2009-07-24 21:23 ` Anselm Helbig
  2009-07-24 21:46 ` Pascal J. Bourguignon
  1 sibling, 0 replies; 5+ messages in thread
From: Anselm Helbig @ 2009-07-24 21:23 UTC (permalink / raw)
  To: help-gnu-emacs

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) ?

In emacs lisp, you'd use fset to set the function value of a symbol:

  (fset 'f (lambda () 2))
  (f)

See (info "(elisp) Function Cells") for more details. 

HTH, 

Anselm


-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: defun vs lambda
  2009-07-24 20:30 defun vs lambda weber
  2009-07-24 21:23 ` Anselm Helbig
@ 2009-07-24 21:46 ` Pascal J. Bourguignon
  2009-07-31 11:41   ` weber
  1 sibling, 1 reply; 5+ messages in thread
From: Pascal J. Bourguignon @ 2009-07-24 21:46 UTC (permalink / raw)
  To: help-gnu-emacs

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__


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: defun vs lambda
  2009-07-24 21:46 ` Pascal J. Bourguignon
@ 2009-07-31 11:41   ` weber
  2009-07-31 12:56     ` Pascal J. Bourguignon
  0 siblings, 1 reply; 5+ messages in thread
From: weber @ 2009-07-31 11:41 UTC (permalink / raw)
  To: help-gnu-emacs

On Jul 24, 6:46 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> weber<hug...@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) "   \\\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) "   \\\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__

Thanks Anselm.

Pascal,

I did wanted to generate code at run-time, similar to the way like
defstruct generates helper functions.

Thanks for the great explanation!
Cheers,
weber


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: defun vs lambda
  2009-07-31 11:41   ` weber
@ 2009-07-31 12:56     ` Pascal J. Bourguignon
  0 siblings, 0 replies; 5+ messages in thread
From: Pascal J. Bourguignon @ 2009-07-31 12:56 UTC (permalink / raw)
  To: help-gnu-emacs

weber <hugows@gmail.com> writes:

> I did wanted to generate code at run-time, similar to the way like
> defstruct generates helper functions.

"Times" are a little complicated matter in lisp.

There is a read-time, a macro-expansion time, a compilation-time, a
load-time and a run-time.  In emacs lisp, things are a little
simplier, since there's no customizable reader macros, there's no way
to have user code executing at read-time.  Nonetheless, there are
means to have code evaluated during each of four other times.

The problem is that more than "times" they are "phases", and they can
occur intermixed one with the other.

When at run-time, (eg in the scratch buffer you run the
eval-last-expression (C-x C-e) command to evaluate the form you just
typed), this form is first read; during this read-time, a lisp object
is built according the syntax of the characters that are "read".  Then
the read object (which must be a form), is evaluated; during this
run-time, it may be noted that we must interpret a macro.  We will
call corresponding macro-function, and we get the body of the macro
executed during this macro expansion time.  The macro may build a new
function and call byte-compile to include a compiled function in the
returned form, so we will have a compilation-time, which further may
include macro-expansion times, etc. At compilation-time it is possible
to set things up to be evaluated at load-time, only when you use load
on the .elc file (see the load-time-value macro in (require 'cl)).

If you compile the file (byte-compile-file), then you go to the
compilation-time, it reads the source (read-time), then macroexpands
the macros (macro-expansion time), then generate the byte code.  You
will later load it (load-time), and then you may execute it
(run-time).  If your program generate *at run-time* new functions, it
may compile them (compilation-time, macro-expansion-time), and then
execute them (back at run-time). etc...


defstruct is a macro.  You may use it in different circumstances,
let's consider the three following cases:

- You directly evaluate (defstruct point x y).

- You put (defstruct point x y) in a source file, 
  and then evaluate (byte-compile-file "point.el"),

- You put it in a function, such as:

     (defun example ()
        (defstruct point x y))

  and you evaluate this form (which defines the function example),
  or you have this function in a file, and you byte-compile-file it.
  Then  you load the file and you call the function (example).


Let's consider the second case.  The compiler will read the defstruct
form, will notice that the symbol defstruct is defined as a macro, and
the will call the corresponding macro-function.  Now at macroexpansion
time, defstruct can work, and generate the accessor functions needed
from the arguments (a list of fields).  Notice that the macro
generates the functions at macro-expansion time, not at run-time.  It
returns a progn form containing defun forms amongst others.  The
compiler then compile that form, including the function definitions,
and generate the byte-code of these functions in a file, along with
the byte-code  needed to _define_ these functions, that is, to
associate the compiled function byte code to the symbol that name the
function.

Later you will load this byte-code file, and will have the function
definition loaded in your emacs lisp image.  No run-time has occured,
yet your functions are already defined.


In the third case, when compiling the defun form, the compiler will
macroexpand the defstruct form, and will generate the body of the
example function, which contain only the instructions needed to
_define_ these function, that is, to associate the compiled function
byte code to the symbol that name the function.

Then you call (example), and at run-time, the accessors of the
structure (which have been generated at macro-expansion time during
compilation of the function example), will be _defined_, that is
associated to the symbol that name the accessor function.  Still, no
code has been generated at run-time.


In the first case, you might have the impression that defstruct
generate code at run-time but as the above explications, and
particularly the third case, may lead you to understand, when
evaluating the defstruct form, there's a macroexpansion time during
which the functions are generated, and then what is executed at
run-time, is only the binding of these functions to their name (when
executing the defun forms).  Even without a compilation time, when
interpreting code, the function generation done by macros is not
exactly done at run-time.


The reason why it is not the case, and why it is not needed to
generate the code at run-time, is that all the information needed to
generate these defstruct accessor functions is known before run-time:
none of the arguments of the defstruct macro are evaluated (at
run-time). They're taken literally, and known at compilation-time.
The macro just returns the accessor function definition, it doesn't
need to return forms that would evaluate the arguments (at run-time)
and generate the functions (at run-time), since it has all the
information to do it itself at macro-expansion time.


Therefore since your sentence is contradictory, I still don't know
when you want to generate your functions...

-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-07-31 12:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-24 20:30 defun vs lambda weber
2009-07-24 21:23 ` Anselm Helbig
2009-07-24 21:46 ` Pascal J. Bourguignon
2009-07-31 11:41   ` weber
2009-07-31 12:56     ` Pascal J. Bourguignon

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.