unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* About `funcall'
@ 2013-03-04 12:52 Xue Fuqiao
  2013-03-04 12:57 ` Xue Fuqiao
       [not found] ` <mailman.21340.1362401871.855.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 7+ messages in thread
From: Xue Fuqiao @ 2013-03-04 12:52 UTC (permalink / raw)
  To: help-gnu-emacs

In `yank-pop', there is an sexp about `funcall':

   (funcall (or yank-undo-function 'delete-region) (point) (mark t))

IIRC the first argument for `funcall' should be a function, so I'm 
confused with this usage.  Can somebody explain it to me?  Thanks.

-- 
Best regards, Xue Fuqiao.
http://www.emacswiki.org/emacs/XueFuqiao



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

* Re: About `funcall'
  2013-03-04 12:52 About `funcall' Xue Fuqiao
@ 2013-03-04 12:57 ` Xue Fuqiao
       [not found] ` <mailman.21340.1362401871.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 7+ messages in thread
From: Xue Fuqiao @ 2013-03-04 12:57 UTC (permalink / raw)
  To: help-gnu-emacs

On 03/04/2013 08:52 PM, Xue Fuqiao wrote:
> In `yank-pop', there is an sexp about `funcall':
>
>    (funcall (or yank-undo-function 'delete-region) (point) (mark t))
>
> IIRC the first argument for `funcall' should be a function, so I'm
> confused with this usage.  Can somebody explain it to me?  Thanks.

Maybe the first argument is whatever the `or' expression
returns, so the remaining arguments are passed to it?

-- 
Best regards, Xue Fuqiao.
http://www.emacswiki.org/emacs/XueFuqiao



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

* Re: About `funcall'
       [not found] ` <mailman.21340.1362401871.855.help-gnu-emacs@gnu.org>
@ 2013-03-04 14:11   ` Michael Heerdegen
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Heerdegen @ 2013-03-04 14:11 UTC (permalink / raw)
  To: help-gnu-emacs

Xue Fuqiao <xfq.free@gmail.com> writes:

> On 03/04/2013 08:52 PM, Xue Fuqiao wrote:
> > In `yank-pop', there is an sexp about `funcall':
> >
> >    (funcall (or yank-undo-function 'delete-region) (point) (mark t))
> >
> > IIRC the first argument for `funcall' should be a function, so I'm
> > confused with this usage.  Can somebody explain it to me?  Thanks.
>
> Maybe the first argument is whatever the `or' expression
> returns, so the remaining arguments are passed to it?

Exactly.  Note that `funcall' is a _function_, so it evaluates all
arguments first.  The first argument should evaluate to a function,
which is then called with the remaining arguments.

This is useful, because unlike in scheme, this:

  ((or yank-undo-function 'delete-region) (point) (mark t))

won't work in Emacs Lisp.

Note that `apply' behaves analogously, because it's also a function.


Regards,

Michael.



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

* Re: About `funcall'
       [not found] <mailman.21339.1362401601.855.help-gnu-emacs@gnu.org>
@ 2013-03-04 19:12 ` Raffaele Ricciardi
  2013-03-04 19:15 ` Pascal J. Bourguignon
  1 sibling, 0 replies; 7+ messages in thread
From: Raffaele Ricciardi @ 2013-03-04 19:12 UTC (permalink / raw)
  To: help-gnu-emacs

On 04/03/13 12:52, Xue Fuqiao wrote:
> In `yank-pop', there is an sexp about `funcall':
>
>    (funcall (or yank-undo-function 'delete-region) (point) (mark t))
>
> IIRC the first argument for `funcall' should be a function, so I'm
> confused with this usage.  Can somebody explain it to me?  Thanks.
>

(or yank-undo-function 'delete-region)

evaluates to a function, which is either the value of yank-undo-function 
or 'delete-region. Compare for instance this snippet, where `add' is a 
variable whose value is the function '+:

(let ((add '+))
   ;; Add if yes, otherwise multiply.
   (funcall (if (yes-or-no-p "Add?") add '*) 4 4))


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

* Re: About `funcall'
       [not found] <mailman.21339.1362401601.855.help-gnu-emacs@gnu.org>
  2013-03-04 19:12 ` Raffaele Ricciardi
@ 2013-03-04 19:15 ` Pascal J. Bourguignon
  1 sibling, 0 replies; 7+ messages in thread
From: Pascal J. Bourguignon @ 2013-03-04 19:15 UTC (permalink / raw)
  To: help-gnu-emacs

Xue Fuqiao <xfq.free@gmail.com> writes:

> In `yank-pop', there is an sexp about `funcall':
>
>   (funcall (or yank-undo-function 'delete-region) (point) (mark t))
>
> IIRC the first argument for `funcall' should be a function, so I'm
> confused with this usage.  Can somebody explain it to me?  Thanks.

`or' returns the first argument that is not nil, or nil when all the
arguments are nil.

If the value bound to yank-undo-function is not nil, then it's returned,
else the symbol delete-region is returned (since it's not nil).

    (or a b) <=> (let ((first-arg a)) 
                   (if first-arg
                      first-arg
                      b))

The temporary variable is needed for (let ((i 0)) (or (incf i) 42))
returns 1, not 2.

    (defmacro .or (&rest args)
      (if (null args)
         'nil
         (let ((var (gensym)))
          `(let ((,var ,(first args)))
             (if ,var
                ,var
                (.or ,@(rest args)))))))

    (macroexpand '(.or a b))
    --> (let ((#1=#:G90861 a)) (if #1# #1# (\.or b)))



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


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

* Re: About `funcall'
@ 2013-03-04 22:59 Xue Fuqiao
  0 siblings, 0 replies; 7+ messages in thread
From: Xue Fuqiao @ 2013-03-04 22:59 UTC (permalink / raw)
  To: help-gnu-emacs

> Xue Fuqiao <xfq.free@gmail.com> writes:
>
>> In `yank-pop', there is an sexp about `funcall':
>>
>>   (funcall (or yank-undo-function 'delete-region) (point) (mark t))
>>
>> IIRC the first argument for `funcall' should be a function, so I'm
>> confused with this usage.  Can somebody explain it to me?  Thanks.
>
> `or' returns the first argument that is not nil, or nil when all the
> arguments are nil.

I see, thanks.

>     (defmacro .or (&rest args)
>       (if (null args)
>          'nil
>          (let ((var (gensym)))
>           `(let ((,var ,(first args)))
>              (if ,var
>                 ,var
>                 (.or ,@(rest args)))))))
>
>     (macroexpand '(.or a b))
>     --> (let ((#1=#:G90861 a)) (if #1# #1# (\.or b)))--

What does "(first args)" mean here?  Does it mean "(car args)"?



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

* Re: About `funcall'
       [not found] <mailman.21404.1362437985.855.help-gnu-emacs@gnu.org>
@ 2013-03-04 23:23 ` Pascal J. Bourguignon
  0 siblings, 0 replies; 7+ messages in thread
From: Pascal J. Bourguignon @ 2013-03-04 23:23 UTC (permalink / raw)
  To: help-gnu-emacs

Xue Fuqiao <xfq.free@gmail.com> writes:

>> Xue Fuqiao <xfq.free@gmail.com> writes:
>>
>>> In `yank-pop', there is an sexp about `funcall':
>>>
>>>   (funcall (or yank-undo-function 'delete-region) (point) (mark t))
>>>
>>> IIRC the first argument for `funcall' should be a function, so I'm
>>> confused with this usage.  Can somebody explain it to me?  Thanks.
>>
>> `or' returns the first argument that is not nil, or nil when all the
>> arguments are nil.
>
> I see, thanks.
>
>>     (defmacro .or (&rest args)
>>       (if (null args)
>>          'nil
>>          (let ((var (gensym)))
>>           `(let ((,var ,(first args)))
>>              (if ,var
>>                 ,var
>>                 (.or ,@(rest args)))))))
>>
>>     (macroexpand '(.or a b))
>>     --> (let ((#1=#:G90861 a)) (if #1# #1# (\.or b)))--
>
> What does "(first args)" mean here?  Does it mean "(car args)"?

Yes.  And (rest args) is the same as (cdr args).

But since I consider here args to be a list, and not a cons cell, then I
use first and rest.  I use car and cdr only when I consider cons cells.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


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

end of thread, other threads:[~2013-03-04 23:23 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-04 12:52 About `funcall' Xue Fuqiao
2013-03-04 12:57 ` Xue Fuqiao
     [not found] ` <mailman.21340.1362401871.855.help-gnu-emacs@gnu.org>
2013-03-04 14:11   ` Michael Heerdegen
     [not found] <mailman.21339.1362401601.855.help-gnu-emacs@gnu.org>
2013-03-04 19:12 ` Raffaele Ricciardi
2013-03-04 19:15 ` Pascal J. Bourguignon
  -- strict thread matches above, loose matches on Subject: below --
2013-03-04 22:59 Xue Fuqiao
     [not found] <mailman.21404.1362437985.855.help-gnu-emacs@gnu.org>
2013-03-04 23:23 ` Pascal J. Bourguignon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).