unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* identity function with an echo side effect
@ 2010-08-10 20:56 bolega
  2010-08-10 23:03 ` Pascal J. Bourguignon
  0 siblings, 1 reply; 7+ messages in thread
From: bolega @ 2010-08-10 20:56 UTC (permalink / raw)
  To: help-gnu-emacs

After searching google groups and emacs apropos extensively, I could
not find a function, perhaps I am missing one that can return
identically return its argument and has a small side effect of echoing
the argument in some place such as for example

the mini buffer or the point at which C-x C-e is typed.

For example, calling this function "echo" , usage would look like
this :

(cdr ( echo (cdr (echo (cdr (echo '(a b c d)))))))

echo:
'(a b c d) or (a b c d)  (I am not sure which would be appropriate)
(b c d)
(c d)

result:
(d)

Is there a need for quotes to prevent evaluation of alphabets at any
phase ?

Thanks.


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

* Re: identity function with an echo side effect
  2010-08-10 20:56 identity function with an echo side effect bolega
@ 2010-08-10 23:03 ` Pascal J. Bourguignon
  2010-08-11  5:01   ` bolega
  0 siblings, 1 reply; 7+ messages in thread
From: Pascal J. Bourguignon @ 2010-08-10 23:03 UTC (permalink / raw)
  To: help-gnu-emacs

bolega <gnuist006@gmail.com> writes:

> After searching google groups and emacs apropos extensively, I could
> not find a function, perhaps I am missing one that can return
> identically return its argument and has a small side effect of echoing
> the argument in some place such as for example
>
> the mini buffer or the point at which C-x C-e is typed.
>
> For example, calling this function "echo" , usage would look like
> this :
>
> (cdr ( echo (cdr (echo (cdr (echo '(a b c d)))))))
>
> echo:
> '(a b c d) or (a b c d)  (I am not sure which would be appropriate)
> (b c d)
> (c d)
>
> result:
> (d)
>
> Is there a need for quotes to prevent evaluation of alphabets at any
> phase ?

In Common Lisp, you can use PRINT for this.


CL-USER> (cdr (print (cdr (print (cdr (print '(a b c d)))))))

(A B C D) 
(B C D) 
(C D) 
(D)
CL-USER> 



In emacs lisp too, but the output of print goes to the *Message* buffer.

M-x ielm RET
ELISP> (cdr (print (cdr (print (cdr (print '(a b c d)))))))
(d)

and you get:

(a b c d)

(b c d)

(c d)

in the minibuffer and in *Message*.


Or, in an emacs lisp buffer:

(cdr (print (cdr (print (cdr (print '(a b c d))))))) C-u C-x C-e

inserts:

(a b c d)

(b c d)

(c d)
(d)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: identity function with an echo side effect
  2010-08-10 23:03 ` Pascal J. Bourguignon
@ 2010-08-11  5:01   ` bolega
  2010-08-11 14:19     ` Pascal J. Bourguignon
  0 siblings, 1 reply; 7+ messages in thread
From: bolega @ 2010-08-11  5:01 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: bolega

On Aug 10, 4:03 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> bolega <gnuist...@gmail.com> writes:
> > After searching google groups and emacs apropos extensively, I could
> > not find a function, perhaps I am missing one that can return
> > identically return its argument and has a small side effect of echoing
> > the argument in some place such as for example
>
> > the mini buffer or the point at which C-x C-e is typed.
>
> > For example, calling this function "echo" , usage would look like
> > this :
>
> > (cdr ( echo (cdr (echo (cdr (echo '(a b c d)))))))
>
> > echo:
> > '(a b c d) or (a b c d)  (I am not sure which would be appropriate)
> > (b c d)
> > (c d)
>
> > result:
> > (d)
>
> > Is there a need for quotes to prevent evaluation of alphabets at any
> > phase ?
>
> In Common Lisp, you can use PRINT for this.
>
> CL-USER> (cdr (print (cdr (print (cdr (print '(a b c d)))))))
>
> (A B C D)
> (B C D)
> (C D)
> (D)
> CL-USER>
>
> In emacs lisp too, but the output of print goes to the *Message* buffer.
>
> M-x ielm RET
> ELISP> (cdr (print (cdr (print (cdr (print '(a b c d)))))))
> (d)
>
> and you get:
>
> (a b c d)
>
> (b c d)
>
> (c d)
>
> in the minibuffer and in *Message*.
>
> Or, in an emacs lisp buffer:
>
> (cdr (print (cdr (print (cdr (print '(a b c d))))))) C-u C-x C-e
>
> inserts:
>
> (a b c d)
>
> (b c d)
>
> (c d)
> (d)
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/

THANKS A LOT !!!

If you dont mind, how many years have you spent on learning emacs/
lisp ?

Can you kindly give a reading syllabus, books list and possibly time
for completion from your perspective and what to get out of each book
since they have some common repetition.

Are there any implicit quotations taking place in the above sequence
of cdr, print ... ?

Bolega



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

* Re: identity function with an echo side effect
  2010-08-11  5:01   ` bolega
@ 2010-08-11 14:19     ` Pascal J. Bourguignon
  2010-08-11 17:42       ` bolega
  0 siblings, 1 reply; 7+ messages in thread
From: Pascal J. Bourguignon @ 2010-08-11 14:19 UTC (permalink / raw)
  To: help-gnu-emacs

bolega <gnuist006@gmail.com> writes:

> On Aug 10, 4:03 pm, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> bolega <gnuist...@gmail.com> writes:
>> > After searching google groups and emacs apropos extensively, I could
>> > not find a function, perhaps I am missing one that can return
>> > identically return its argument and has a small side effect of echoing
>> > the argument in some place such as for example
>>
>> > the mini buffer or the point at which C-x C-e is typed.
>>
>> > For example, calling this function "echo" , usage would look like
>> > this :
>>
>> > (cdr ( echo (cdr (echo (cdr (echo '(a b c d)))))))
>>
>> > echo:
>> > '(a b c d) or (a b c d)  (I am not sure which would be appropriate)
>> > (b c d)
>> > (c d)
>>
>> > result:
>> > (d)
>>
>> > Is there a need for quotes to prevent evaluation of alphabets at any
>> > phase ?

No.  Expressions are evaluated only by EVAL or LOAD (and at
compilation time, compilation time expressions by COMPILE and
COMPILE-FILE).  Otherwise, values are just values, they don't get
evaluated magically (for what reason should they?)


>> In Common Lisp, you can use PRINT for this.
>>
>> CL-USER> (cdr (print (cdr (print (cdr (print '(a b c d)))))))
>>
>> (A B C D)
>> (B C D)
>> (C D)
>> (D)
>> CL-USER>
>> [...]
>
> If you dont mind, how many years have you spent on learning emacs/
> lisp ?

20 years.


> Can you kindly give a reading syllabus, books list and possibly time
> for completion from your perspective and what to get out of each book
> since they have some common repetition.

http://www.cliki.net/admin/search?words=books

In particular, I like to advise:

 Common Lisp: A Gentle Introduction to Symbolic Computation
 http://www-cgi.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/LispBook/index.html
 http://www.cs.cmu.edu/~dst/LispBook/



> Are there any implicit quotations taking place in the above sequence
> of cdr, print ... ?

Since CDR and PRINT are function, no implicit quotation occurs: the
result values are directly passed as argument to the next function.

Only macros receive their arguments un-evaluated, which could be
characterized as an "implicit quotation".


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: identity function with an echo side effect
  2010-08-11 14:19     ` Pascal J. Bourguignon
@ 2010-08-11 17:42       ` bolega
  2010-08-11 20:43         ` Pascal J. Bourguignon
  2010-08-12  2:35         ` TheFlyingDutchman
  0 siblings, 2 replies; 7+ messages in thread
From: bolega @ 2010-08-11 17:42 UTC (permalink / raw)
  To: help-gnu-emacs

On Aug 11, 7:19 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> bolega <gnuist...@gmail.com> writes:
> > On Aug 10, 4:03 pm, p...@informatimago.com (Pascal J. Bourguignon)
> > wrote:
> >> bolega <gnuist...@gmail.com> writes:
> >> > After searching google groups and emacs apropos extensively, I could
> >> > not find a function, perhaps I am missing one that can return
> >> > identically return its argument and has a small side effect of echoing
> >> > the argument in some place such as for example
>
> >> > the mini buffer or the point at which C-x C-e is typed.
>
> >> > For example, calling this function "echo" , usage would look like
> >> > this :
>
> >> > (cdr ( echo (cdr (echo (cdr (echo '(a b c d)))))))
>
> >> > echo:
> >> > '(a b c d) or (a b c d)  (I am not sure which would be appropriate)
> >> > (b c d)
> >> > (c d)
>
> >> > result:
> >> > (d)
>
> >> > Is there a need for quotes to prevent evaluation of alphabets at any
> >> > phase ?
>
> No.  Expressions are evaluated only by EVAL or LOAD (and at
> compilation time, compilation time expressions by COMPILE and
> COMPILE-FILE).  Otherwise, values are just values, they don't get
> evaluated magically (for what reason should they?)
>
> >> In Common Lisp, you can use PRINT for this.
>
> >> CL-USER> (cdr (print (cdr (print (cdr (print '(a b c d)))))))
>
> >> (A B C D)
> >> (B C D)
> >> (C D)
> >> (D)
> >> CL-USER>
> >> [...]
>
> > If you dont mind, how many years have you spent on learning emacs/
> > lisp ?
>
> 20 years.
>
> > Can you kindly give a reading syllabus, books list and possibly time
> > for completion from your perspective and what to get out of each book
> > since they have some common repetition.
>
> http://www.cliki.net/admin/search?words=books
>
> In particular, I like to advise:
>
>  Common Lisp: A Gentle Introduction to Symbolic Computation
>  http://www-cgi.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/LispBook/index....
>  http://www.cs.cmu.edu/~dst/LispBook/
>
> > Are there any implicit quotations taking place in the above sequence
> > of cdr, print ... ?
>
> Since CDR and PRINT are function, no implicit quotation occurs: the
> result values are directly passed as argument to the next function.

Then why does the following gives an error. Currently, I only have
elisp working so we confine to emacs runs.

(cdr (print (cdr (print (cdr (print (a b c d)))))))    C-x C-e

gives this error if I remove the quote. why ? I get errors if I remove
all prints. This means if the first cdr required quoted list, then the
rest must also require it. Hence, an implicit quotation might be
occurring as in setq ? Also, plz explain me the debugger output in
detail so I can figure it out myself. may be a line by line comment
and if there is a more comprehensive example you can cook, better for
all the readers, once and for all.

Debugger entered--Lisp error: (void-function a)
  (a b c d)
  (print (a b c d))
  (cdr (print (a b c d)))
  (print (cdr (print ...)))
  (cdr (print (cdr ...)))
  (print (cdr (print ...)))
  (cdr (print (cdr ...)))
  eval((cdr (print (cdr ...))))
  eval-last-sexp-1(nil)
  eval-last-sexp(nil)
* call-interactively(eval-last-sexp)



>
> Only macros receive their arguments un-evaluated, which could be
> characterized as an "implicit quotation".
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/



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

* Re: identity function with an echo side effect
  2010-08-11 17:42       ` bolega
@ 2010-08-11 20:43         ` Pascal J. Bourguignon
  2010-08-12  2:35         ` TheFlyingDutchman
  1 sibling, 0 replies; 7+ messages in thread
From: Pascal J. Bourguignon @ 2010-08-11 20:43 UTC (permalink / raw)
  To: help-gnu-emacs

bolega <gnuist006@gmail.com> writes:

> On Aug 11, 7:19 am, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> Since CDR and PRINT are function, no implicit quotation occurs: the
>> result values are directly passed as argument to the next function.
>
> Then why does the following gives an error. Currently, I only have
> elisp working so we confine to emacs runs.
>
> (cdr (print (cdr (print (cdr (print (a b c d)))))))    C-x C-e
>
> gives this error if I remove the quote. why ? 

What are the rules of lisp evaluation?


> I get errors if I remove
> all prints. This means if the first cdr required quoted list, 

No.  cdr requires a LIST.  Nothing more, nothing less.


> then the rest must also require it. 

print may take any lisp object as argument.  cdr mak take any list.


> Hence, an implicit quotation might be
> occurring as in setq ? 

setq as its name implies indeed makes an implicit quotation.  But not
the one you may think. 


> Also, plz explain me the debugger output in
> detail so I can figure it out myself. may be a line by line comment
> and if there is a more comprehensive example you can cook, better for
> all the readers, once and for all.
>
> Debugger entered--Lisp error: (void-function a)

a is not a function, that is, the function slot of the symbol a is void.

The rest is the backtrace:

The error was detected when trying to evaluate this form:


>   (a b c d)

which was to be evaluated because it was needed to evaluate this form:


>   (print (a b c d))

etc.


>   (cdr (print (a b c d)))
>   (print (cdr (print ...)))
>   (cdr (print (cdr ...)))
>   (print (cdr (print ...)))
>   (cdr (print (cdr ...)))
>   eval((cdr (print (cdr ...))))
>   eval-last-sexp-1(nil)
>   eval-last-sexp(nil)
> * call-interactively(eval-last-sexp)





The rules of evaluation of lisp are something like:


    (defun eval. (form)
      (cond
        ((symbolp form) (symbol-value form))
        ((atom form)    form)
        (t (case (first form)
             ((quote function)   (second form))
             ((if)  (if (eval. (second form))
                        (eval. (third  form))
                        (let ((result))
                           (dolist (subform (cdddr form))
                             (setf result (eval. subform)))
                           result)))

             ;; ... other special operator special rules here ...

             (otherwise
                (cond ((symbol-function (first form))
                       (apply. (symbol-function (first form))
                              (mapcar (function eval.) (rest form))))
                      ((macro-function (first form))
                       (eval. (macroexpand form)))
                      (t
                        (error "(void-function %S)" (first form)))))))))



Quote is useless and worthless, you can write your programs without it.

To get the symbol named "a", you may use the function intern.
To build a list, you may use the function list.

    (let ((my-list (list (intern "a")
                         (intern "b")
                         (intern "c")
                         (intern "d"))))
      (cdr (print (cdr (print (cdr (print my-list)))))))

    (a b c d)

    (b c d)

    (c d)
    (d)


It just happen that when you read "(a b c d)", the lisp reader just
calls intern and list like you would.  So you could also write, in
emacs lisp:


    (let ((my-list (car (read-from-string "(a b c d)"))))
      (cdr (print (cdr (print (cdr (print my-list)))))))


(in Common Lisp, READ-FROM-STRING returns two values, instead of
returning one cons cell with the two values, so the CAR would be
superfluous).

But also, it happens that:

   (car (read-from-string "(a b c d)"))
returns:
   (a b c d)

and that lisp programs are read by the same lisp reader used by
read-from-string.  Therefore if you write 

   (a b c d)

in your program, what will be read is exactly the same as the list you
would build with list and intern, or read with read-from-string.

There's just a little problem, that what is read in a program is also
evaluated!  And (a b c d) is not a symbol, it's not an atom, and its
first element is not quote, if, or any other special operator defined
in lisp,  therefore eval will check if it is a function.  Since a is
not defined as a function, and it is not defined as a macro, eval will
fall down in the latest case, were it will report the error that a is
not a function.

Any expression in a program will be executed!  This is the point of
being a program, to be executed (by eval).

So how can we insert in a program some literal data.  For example,
what happens when we evaluate the expression:
  
   42

?

It is not a symbol.  But it is an atom.  And when it's an atom, eval
will return it as is.  non-symbol atoms are self-evaluating.
Therefore the value (the result of evaluating) 42 is 42.  We're quite
lucky that numbers are worth themselves!

For non atoms, we must use the special operator quote, because
otherwise they would be evaluated as an operator application.

  (quote 42)

is not a symbol, is not an atom (it's a cons cell), but it's first
element is the symbol quote.  In that case, eval returns the second
elements, that is, 42. Therefore:

  (quote 42)

evaluates to

  42

which is the same as what:

  42

evaluates to.

(eql '42 42)  evaluates to true!   But this is something that is
specific to non-symbol atoms (and some symbols, that are bound to
themselves such as nil, t, or any other variable you'd bind to their
symbol).



  (quote (a b c d))

is not a symbol, is not an atom, is a list whose first element is
quote, therefore eval returns the second elements, which happens to be
the list:

  (a b c d)


Therefore   (quote (a b c d))  evaluates to:

  (a b c d)


Hence we have a way to produce a literal list in a program, by using quote.

    (cdr (print (cdr (print (cdr (print (quote (a b c d))))))))


    (a b c d)

    (b c d)

    (c d)
    (d)


But there is no quoted lists, no more that there is any quoted number
or quoted string or quoted anything.   The only thing there is, is
some literal data in a program.  And you should not modify any literal
data because that would mean modifying the program in some
uncontrolled way, and you'd get unpreditable results.  But apart from
this, literal data is data like any other data.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: identity function with an echo side effect
  2010-08-11 17:42       ` bolega
  2010-08-11 20:43         ` Pascal J. Bourguignon
@ 2010-08-12  2:35         ` TheFlyingDutchman
  1 sibling, 0 replies; 7+ messages in thread
From: TheFlyingDutchman @ 2010-08-12  2:35 UTC (permalink / raw)
  To: help-gnu-emacs


> (cdr (print (cdr (print (cdr (print (a b c d)))))))    C-x C-e
>
> gives this error if I remove the quote. why ? I get errors if I remove
> all prints. This means if the first cdr required quoted list, then the
> rest must also require it. Hence, an implicit quotation might be
> occurring as in setq ?

Each cdr requires the same thing - a list object. The first cdr does
not receive "(a b c d)" as its argument, it receives that list literal
turned into its in-memory representation as a  list object. Each cdr
returns a list object (the internal format - not a string literal) to
the next cdr.

The Emacs Lisp interpreter has to turn the (a b c d) "list literal"
into a list object before passing it to the first cdr function.
However, Emacs Lisp has a problem that most languages do not have. A
list literal looks exactly the same as a function call. e.g.:

(min a b)   => Emacs Lisp function call   OR Emacs Lisp list literal

The above could be a list, with symbols min, a and b. Or it could be a
function call of min with parameters a and b. In contrast, the Python
interpreter would not have a problem differentiating between the two
because there is different syntax for each:

min(a ,b)  => Python function call   [min, a, b]   =>  Python list
literal

So how does the Emacs Lisp interpreter differentiate between a
function call and a list literal? It doesn't. It assumes everything is
a function call - or similar expression (macro, special form). If you
are actually writing a list literal and not an expression to evaluate,
you have to explicitly tell the Emacs Lisp interpreter not to evaluate
it as an expression, but to just store it as a list object.

so (a b c d) to Emacs Lisp is a call to the function of symbol a, and
it should pass b, c and d as parameters.   '(a b c d)  or (quote (a b
c d)) tells Emacs Lisp not to try and execute any function stored in a
- just store the whole thing as a list internally.


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

end of thread, other threads:[~2010-08-12  2:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-10 20:56 identity function with an echo side effect bolega
2010-08-10 23:03 ` Pascal J. Bourguignon
2010-08-11  5:01   ` bolega
2010-08-11 14:19     ` Pascal J. Bourguignon
2010-08-11 17:42       ` bolega
2010-08-11 20:43         ` Pascal J. Bourguignon
2010-08-12  2:35         ` TheFlyingDutchman

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