* (list) and '(list)
@ 2007-04-26 15:17 A Soare
2007-04-27 10:25 ` Kai Grossjohann
[not found] ` <mailman.2588.1177669955.7795.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 10+ messages in thread
From: A Soare @ 2007-04-26 15:17 UTC (permalink / raw)
To: Emacs Help [help-gnu-emacs]
Can somebody tell me if there already is a code that makes the distintion between a list that evaluates as a function like
(function ...
and between a list that evaluates to itself :
'(list ... , and its aliases like (quote (list ... etc
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
[not found] <mailman.2557.1177600988.7795.help-gnu-emacs@gnu.org>
@ 2007-04-26 21:55 ` Pascal Bourguignon
2007-04-27 0:58 ` Tim X
1 sibling, 0 replies; 10+ messages in thread
From: Pascal Bourguignon @ 2007-04-26 21:55 UTC (permalink / raw)
To: help-gnu-emacs
A Soare <alinsoar@voila.fr> writes:
> Can somebody tell me if there already is a code that makes the
> distintion between a list that evaluates as a function like
>
> (function ...
>
> and between a list that evaluates to itself :
>
> '(list ... , and its aliases like (quote (list ... etc
A list that evaluates to itself? It cannot start with list. The
function list returns a NEW list, so it will never return the source
list itself.
In your question, there's also a problem with the definition of
'itself'. Most quine programs (which are represented as lists, so
they'd be candidates), don't return themselves (in the sense of eq),
but themselves in the sense of equal.
An easy way to know if the variable x is bound to a list that
evaluates to itself in the sense of equal is:
(and (listp x) (equal x (eval x)))
For example:
(let ((x '((lambda nil
(let ((src "((lambda nil
(let ((src %S))
(car (read-from-string (format src src))))))"))
(car (read-from-string (format src src))))))))
(and (listp x) (equal x (eval x))))
--> t
If you really want to test lists that give themselves in the sense of
eq when evaluated, this restricts the number of such lists a lot, and
you have to use #= and ## to write them:
(let ((x '#1=((lambda nil (quote #1#)))))
(and (listp x) (eq x (eval x))))
--> t
Oh, and there is no way to know what a list evaluates to without
effectively evaluating it, in general (testing whether the car of the
list is list or function is evaluating it, at least partially. If you
want an exact result, you'd need to evaluate it completely).
--
__Pascal Bourguignon__ http://www.informatimago.com/
PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
[not found] <mailman.2557.1177600988.7795.help-gnu-emacs@gnu.org>
2007-04-26 21:55 ` Pascal Bourguignon
@ 2007-04-27 0:58 ` Tim X
1 sibling, 0 replies; 10+ messages in thread
From: Tim X @ 2007-04-27 0:58 UTC (permalink / raw)
To: help-gnu-emacs
A Soare <alinsoar@voila.fr> writes:
> Can somebody tell me if there already is a code that makes the distintion between a list that evaluates as a function like
>
> (function ...
>
> and between a list that evaluates to itself :
>
> '(list ... , and its aliases like (quote (list ... etc
>
First, you have it around the wrong way. ' is the shorthand (alias if you like)
for (quote .....). If you ignore ', then a clear pattern emerges. Essentially,
all lists are evaluated when viewed from the top level. The (quote ....) is
different in that it short circuits evaluation - it returns its arguements
without evaluating them. In all other situations, the arguements are evaluated.
Putting it another way, given any list
(x y z)
x is always evaluated, but if x is the function quote, evaluation halts there.
otherwise, y and z are evaluated to determine the arguements to x.
I suspect you may be asking thw rong question. Maybe explain more about what
you want to do and others on the list will be able to give you better
advice/direction.
Tim
--
tcross (at) rapttech dot com dot au
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
2007-04-26 15:17 A Soare
@ 2007-04-27 10:25 ` Kai Grossjohann
[not found] ` <mailman.2588.1177669955.7795.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 10+ messages in thread
From: Kai Grossjohann @ 2007-04-27 10:25 UTC (permalink / raw)
To: help-gnu-emacs
A Soare <alinsoar@voila.fr> writes:
> Can somebody tell me if there already is a code that makes the
> distintion between a list that evaluates as a function like
>
> (function ...
>
> and between a list that evaluates to itself :
>
> '(list ... , and its aliases like (quote (list ... etc
Like the others who responded, it is not clear to me what you want.
Lisp normally reads code and then evaluates (evals) it. During
reading, it performs some (few) conversions such as 'foo to (quote
foo). During evaluation, the magic happens:
- If we are looking at a number or a string, return that number or
string. (Numbers and strings are said to be self-evaluating because
they evaluate to themselves.) If we are looking at some special
symbols (like t and nil), return those values. (t and nil are also
self-evaluating.)
- If we are looking at a list, look at the first element of the list.
If it is a function, then eval all other elements of the list, then
use the results as arguments to the function (call the function).
Then return the result of the function call.
- The first element can also be a special form. In this case, the
other elements of the list are NOT evaled but instead passed to the
special form verbatim.
Popular special forms: defun, quote, if, setq
So in a strict sense, what you can do is:
(setq unevaled (read STREAM))
(setq evaled (eval unevaled))
(equal unevaled evaled)
This is, read it first, then eval it and compare whether the result of
reading and the result of evaling are equal.
Perhaps what you want is to write a special form yourself? That can't
be done in a strict sense, but you can use macros to approximate the
effect.
For example, to reverse the condition of `if' (proof of concept only,
not production code):
(defmacro ifnot (test no yes)
(if (not test) no yes))
Does that help?
Kai
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
@ 2007-04-27 10:37 A Soare
0 siblings, 0 replies; 10+ messages in thread
From: A Soare @ 2007-04-27 10:37 UTC (permalink / raw)
To: Kai Grossjohann; +Cc: Emacs Help [help-gnu-emacs]
> The following message is a courtesy copy of an article
> that has been posted to gmane.emacs.help as well.
>
> A Soare <alinsoar@voila.fr> writes:
>
> > Can somebody tell me if there already is a code that makes the
> > distintion between a list that evaluates as a function like
> >
> > (function ...
> >
> > and between a list that evaluates to itself :
> >
> > '(list ... , and its aliases like (quote (list ... etc
>
> Like the others who responded, it is not clear to me what you want.
>
> Lisp normally reads code and then evaluates (evals) it. During
> reading, it performs some (few) conversions such as 'foo to (quote
> foo). During evaluation, the magic happens:
>
> - If we are looking at a number or a string, return that number or
> string. (Numbers and strings are said to be self-evaluating because
> they evaluate to themselves.) If we are looking at some special
> symbols (like t and nil), return those values. (t and nil are also
> self-evaluating.)
>
> - If we are looking at a list, look at the first element of the list.
> If it is a function, then eval all other elements of the list, then
> use the results as arguments to the function (call the function).
> Then return the result of the function call.
>
> - The first element can also be a special form. In this case, the
> other elements of the list are NOT evaled but instead passed to the
> special form verbatim.
>
> Popular special forms: defun, quote, if, setq
>
> So in a strict sense, what you can do is:
>
> (setq unevaled (read STREAM))
> (setq evaled (eval unevaled))
> (equal unevaled evaled)
That is what I wanted to know. Giving a string/[part of a buffer] that represents a LISP code, to say if a list inside it defines a function or a symbol (that evaluates to itself).
That code solves my problem. Thank you very much.
Alin Soare.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
[not found] ` <mailman.2588.1177669955.7795.help-gnu-emacs@gnu.org>
@ 2007-04-27 10:39 ` David Kastrup
2007-04-27 11:05 ` Pascal Bourguignon
1 sibling, 0 replies; 10+ messages in thread
From: David Kastrup @ 2007-04-27 10:39 UTC (permalink / raw)
To: help-gnu-emacs
Kai Grossjohann <kai@emptydomain.de> writes:
> Perhaps what you want is to write a special form yourself? That
> can't be done in a strict sense, but you can use macros to
> approximate the effect.
>
> For example, to reverse the condition of `if' (proof of concept only,
> not production code):
>
> (defmacro ifnot (test no yes)
> (if (not test) no yes))
>
> Does that help?
You are confusing `defmacro' with `defsubst'. What you wrote above
will have the effect of _compiling_ to no if the _unevaluated_ first
argument is anything but nil, and to yes if it is nil.
That is:
(ifnot blurb "x" "y")
will (at compile time already) be resolved to "x", regardless whether
blurb is currently (or at run time) nil. In fact, the _value_ of
blurb is _never_ consulted. To compile to "y", use one of the
following:
(ifnot nil "x" "y")
(ifnot () "x" "y")
What will _not_ work is
(ifnot '() "x" "y")
Because then test will be set to (quote nil) when the test is done,
which is not nil, even though it evaluates to nil.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
[not found] ` <mailman.2588.1177669955.7795.help-gnu-emacs@gnu.org>
2007-04-27 10:39 ` David Kastrup
@ 2007-04-27 11:05 ` Pascal Bourguignon
2007-04-30 12:49 ` Kai Grossjohann
1 sibling, 1 reply; 10+ messages in thread
From: Pascal Bourguignon @ 2007-04-27 11:05 UTC (permalink / raw)
To: help-gnu-emacs
Kai Grossjohann <kai@emptydomain.de> writes:
> For example, to reverse the condition of `if' (proof of concept only,
> not production code):
>
> (defmacro ifnot (test no yes)
> (if (not test) no yes))
(defmacro ifnot (test no yes)
`(if (not ,test) ,no ,yes))
or better:
(defmacro ifnot (test no yes)
`(if ,test ,yes ,no)) ; ...
--
__Pascal Bourguignon__ http://www.informatimago.com/
This is a signature virus. Add me to your signature and help me to live.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
2007-04-27 11:05 ` Pascal Bourguignon
@ 2007-04-30 12:49 ` Kai Grossjohann
0 siblings, 0 replies; 10+ messages in thread
From: Kai Grossjohann @ 2007-04-30 12:49 UTC (permalink / raw)
To: help-gnu-emacs
Pascal Bourguignon <pjb@informatimago.com> writes:
> Kai Grossjohann <kai@emptydomain.de> writes:
>> For example, to reverse the condition of `if' (proof of concept only,
>> not production code):
>>
>> (defmacro ifnot (test no yes)
>> (if (not test) no yes))
>
> (defmacro ifnot (test no yes)
> `(if (not ,test) ,no ,yes))
*blush*
Kai
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
@ 2007-04-30 18:15 A Soare
0 siblings, 0 replies; 10+ messages in thread
From: A Soare @ 2007-04-30 18:15 UTC (permalink / raw)
To: Emacs Help [help-gnu-emacs]
Now the utility of my question is the following.
I rewrote the indentation of lisp code (I am still working on, but the base is done). There are still problems. Look one:
(+ 1 2 3
4 5 6)
This is a list that calls a function. Now this case:
(setq a '(a b c
d e f)
With the current indentation, we would have:
(setq a '(a b c
d e f)
I do not like this. I want to indent for the quoted lists so:
(setq a '(a b c
d e f)
So I need the simplesc code that detects whether a list is commented or not.
Alin Soare.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: (list) and '(list)
@ 2007-04-30 18:53 A Soare
0 siblings, 0 replies; 10+ messages in thread
From: A Soare @ 2007-04-30 18:53 UTC (permalink / raw)
To: Emacs Help [help-gnu-emacs]
I will use something like that:
(memq (caar (read-from-string "(function (list list))")) '(function quote))
This is an answer to my problem.
If you have better solutions, please tell me.
Alin Soare.
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2007-04-30 18:53 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-30 18:15 (list) and '(list) A Soare
-- strict thread matches above, loose matches on Subject: below --
2007-04-30 18:53 A Soare
2007-04-27 10:37 A Soare
[not found] <mailman.2557.1177600988.7795.help-gnu-emacs@gnu.org>
2007-04-26 21:55 ` Pascal Bourguignon
2007-04-27 0:58 ` Tim X
2007-04-26 15:17 A Soare
2007-04-27 10:25 ` Kai Grossjohann
[not found] ` <mailman.2588.1177669955.7795.help-gnu-emacs@gnu.org>
2007-04-27 10:39 ` David Kastrup
2007-04-27 11:05 ` Pascal Bourguignon
2007-04-30 12:49 ` Kai Grossjohann
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).