all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Creating a list
@ 2009-11-19  7:38 Cecil Westerhof
  2009-11-19  8:35 ` David Kastrup
  0 siblings, 1 reply; 12+ messages in thread
From: Cecil Westerhof @ 2009-11-19  7:38 UTC (permalink / raw
  To: help-gnu-emacs

At the moment I create a list with:
      (setq ret-val (cons total-amount (cons current-amount ())))
I thought about doing it with:
      (setq ret-val (cons total-amount '(current-amount)))

But then the second value is the string current-amount instead of the
value of current-amount. Is there a better way to build the list? At
this moment it is not a problem, but when I want to build a list from 30
values ...

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


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

* Re: Creating a list
  2009-11-19  7:38 Creating a list Cecil Westerhof
@ 2009-11-19  8:35 ` David Kastrup
  2009-11-19 10:59   ` Cecil Westerhof
  2009-11-19 11:59   ` Gnus: Some new mails are automatically marked as 'O' Wang Lei
  0 siblings, 2 replies; 12+ messages in thread
From: David Kastrup @ 2009-11-19  8:35 UTC (permalink / raw
  To: help-gnu-emacs

Cecil Westerhof <Cecil@decebal.nl> writes:

> At the moment I create a list with:
>       (setq ret-val (cons total-amount (cons current-amount ())))
> I thought about doing it with:
>       (setq ret-val (cons total-amount '(current-amount)))
>
> But then the second value is the string current-amount

Wrong.  The _symbol_ current-amount.

> instead of the value of current-amount. Is there a better way to build
> the list? At this moment it is not a problem, but when I want to build
> a list from 30 values ...

I would really recommend taking a look at the Introduction to Elisp
Programming

(info "(eintr)")
Press C-x C-e here --^

This is sort of obvious:

(setq ret-val (list total-amount current-amount))

If you have a large piece of structure around the variable values and
would prefer to write it in its unevaluated form, you can also use

(setq ret-val `(,total-amount ,current-amount))

namely backquote the entire structure, and put a comma before everything
within the structure that is to evaluated instead of quoted.  After
byte-compilation, the resulting code is rather the same.

-- 
David Kastrup


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

* Re: Creating a list
  2009-11-19  8:35 ` David Kastrup
@ 2009-11-19 10:59   ` Cecil Westerhof
  2009-11-19 11:41     ` David Kastrup
  2009-11-19 11:59   ` Gnus: Some new mails are automatically marked as 'O' Wang Lei
  1 sibling, 1 reply; 12+ messages in thread
From: Cecil Westerhof @ 2009-11-19 10:59 UTC (permalink / raw
  To: help-gnu-emacs

David Kastrup <dak@gnu.org> writes:

>> At the moment I create a list with:
>>       (setq ret-val (cons total-amount (cons current-amount ())))
>> I thought about doing it with:
>>       (setq ret-val (cons total-amount '(current-amount)))
>>
>> But then the second value is the string current-amount
>
> Wrong.  The _symbol_ current-amount.

When evaluating I got:
    (1570378.2570192777 current-amount)
That is why I thought I got the string.


> I would really recommend taking a look at the Introduction to Elisp
> Programming
>
> (info "(eintr)")
> Press C-x C-e here --^

I'll do that.


> This is sort of obvious:
>
> (setq ret-val (list total-amount current-amount))

Works like a charm.

For the curious. This is the function I wrote:
    (defun arithmetic-row(start-amount interest years)
      (let ((current-amount start-amount)
            (index 1)
            (multiplication-factor (+ 1 (/ interest 100.0)))
            (ret-val)
            (total-amount start-amount))
        (if (< years 1)
            (setq ret-val nil)
          (while (< index years)
            (setq index (1+ index))
            (setq current-amount (* current-amount multiplication-factor))
            (setq total-amount (+ total-amount current-amount)))
          (setq ret-val (list total-amount current-amount)))))

And it can be called with:
    (arithmetic-row 28000 4 30)

With the call it is calculated how much someone earns in total in 30
years when he starts with 28000 a year and get every year a raise of
4%. This total is the first value of the list, the amount earned in the
last year is the second value of the list.

This is a arithmetic solution. But for example in year 3 now is earned
31496.192000. In real life this would be 31496.19. So before using the
value it should be rounded, but until now I did not a round
function. But maybe I'll find that in the intro.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


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

* Re: Creating a list
  2009-11-19 10:59   ` Cecil Westerhof
@ 2009-11-19 11:41     ` David Kastrup
  2009-11-19 13:13       ` Cecil Westerhof
  0 siblings, 1 reply; 12+ messages in thread
From: David Kastrup @ 2009-11-19 11:41 UTC (permalink / raw
  To: help-gnu-emacs

Cecil Westerhof <Cecil@decebal.nl> writes:

> David Kastrup <dak@gnu.org> writes:
>
>>> At the moment I create a list with:
>>>       (setq ret-val (cons total-amount (cons current-amount ())))
>>> I thought about doing it with:
>>>       (setq ret-val (cons total-amount '(current-amount)))
>>>
>>> But then the second value is the string current-amount
>>
>> Wrong.  The _symbol_ current-amount.
>
> When evaluating I got:
>     (1570378.2570192777 current-amount)
> That is why I thought I got the string.

A string would have quote marks around it.

-- 
David Kastrup


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

* Gnus: Some new mails are automatically marked as 'O'
  2009-11-19  8:35 ` David Kastrup
  2009-11-19 10:59   ` Cecil Westerhof
@ 2009-11-19 11:59   ` Wang Lei
  1 sibling, 0 replies; 12+ messages in thread
From: Wang Lei @ 2009-11-19 11:59 UTC (permalink / raw
  To: help-gnu-emacs

Hi, all.

I use gnus as email client. And I have a problem. The new received mail is supposed to be unmarked. But Some of them are marked as 'O', and the group which it belongs doesn't show automatically. When checking the mail, I have to type 'L' first, and then find the new mail.

There is something wrong, isn't it?

-- 
Regards,
Lei




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

* Re: Creating a list
  2009-11-19 11:41     ` David Kastrup
@ 2009-11-19 13:13       ` Cecil Westerhof
  2009-11-19 13:47         ` Barry Margolin
                           ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Cecil Westerhof @ 2009-11-19 13:13 UTC (permalink / raw
  To: help-gnu-emacs

David Kastrup <dak@gnu.org> writes:

>>>> At the moment I create a list with:
>>>>       (setq ret-val (cons total-amount (cons current-amount ())))
>>>> I thought about doing it with:
>>>>       (setq ret-val (cons total-amount '(current-amount)))
>>>>
>>>> But then the second value is the string current-amount
>>>
>>> Wrong.  The _symbol_ current-amount.
>>
>> When evaluating I got:
>>     (1570378.2570192777 current-amount)
>> That is why I thought I got the string.
>
> A string would have quote marks around it.

Off course.

But I have a few questions on my mind.
- Why is the first shown as value and the second as _symbol_?
- current-amount is a local variable. How is it possible that the symbol
  is still defined?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


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

* Re: Creating a list
  2009-11-19 13:13       ` Cecil Westerhof
@ 2009-11-19 13:47         ` Barry Margolin
  2009-11-19 15:11           ` Cecil Westerhof
  2009-11-19 13:57         ` David Kastrup
  2009-11-19 13:59         ` Pascal J. Bourguignon
  2 siblings, 1 reply; 12+ messages in thread
From: Barry Margolin @ 2009-11-19 13:47 UTC (permalink / raw
  To: help-gnu-emacs

In article <873a4afyi0.fsf@Traian.DecebalComp>,
 Cecil Westerhof <Cecil@decebal.nl> wrote:

> David Kastrup <dak@gnu.org> writes:
> 
> >>>> At the moment I create a list with:
> >>>>       (setq ret-val (cons total-amount (cons current-amount ())))
> >>>> I thought about doing it with:
> >>>>       (setq ret-val (cons total-amount '(current-amount)))
> >>>>
> >>>> But then the second value is the string current-amount
> >>>
> >>> Wrong.  The _symbol_ current-amount.
> >>
> >> When evaluating I got:
> >>     (1570378.2570192777 current-amount)
> >> That is why I thought I got the string.
> >
> > A string would have quote marks around it.
> 
> Off course.

I think you mean "of course".

> 
> But I have a few questions on my mind.
> - Why is the first shown as value and the second as _symbol_?

Because you quoted the list containing the symbol, so it wasn't 
evaluated.

> - current-amount is a local variable. How is it possible that the symbol
>   is still defined?

Symbols are permanent, only bindings are local.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


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

* Re: Creating a list
  2009-11-19 13:13       ` Cecil Westerhof
  2009-11-19 13:47         ` Barry Margolin
@ 2009-11-19 13:57         ` David Kastrup
  2009-11-19 15:14           ` Cecil Westerhof
  2009-11-19 13:59         ` Pascal J. Bourguignon
  2 siblings, 1 reply; 12+ messages in thread
From: David Kastrup @ 2009-11-19 13:57 UTC (permalink / raw
  To: help-gnu-emacs

Cecil Westerhof <Cecil@decebal.nl> writes:

> David Kastrup <dak@gnu.org> writes:
>
>>>>> At the moment I create a list with:
>>>>>       (setq ret-val (cons total-amount (cons current-amount ())))
>>>>> I thought about doing it with:
>>>>>       (setq ret-val (cons total-amount '(current-amount)))
>>>>>
>>>>> But then the second value is the string current-amount
>>>>
>>>> Wrong.  The _symbol_ current-amount.
>>>
>>> When evaluating I got:
>>>     (1570378.2570192777 current-amount)
>>> That is why I thought I got the string.
>>
>> A string would have quote marks around it.
>
> Off course.
>
> But I have a few questions on my mind.
> - Why is the first shown as value and the second as _symbol_?

Because you quoted the entire list containing the second value.

> - current-amount is a local variable. How is it possible that the symbol
>   is still defined?

I really recommend that you read a Lisp introduction.  Your questions
are very very basic.

A symbol, like a plain unquoted list, _is_ a valid expression all by
itself.  In the context of evaluation, a further interpretation tries
going from there (fetching a symbol's value, calling the first element
of a list as a function on the rest).  But whether or not you can
usefully call eval on it, either are printable and useful expressions.

-- 
David Kastrup


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

* Re: Creating a list
  2009-11-19 13:13       ` Cecil Westerhof
  2009-11-19 13:47         ` Barry Margolin
  2009-11-19 13:57         ` David Kastrup
@ 2009-11-19 13:59         ` Pascal J. Bourguignon
  2 siblings, 0 replies; 12+ messages in thread
From: Pascal J. Bourguignon @ 2009-11-19 13:59 UTC (permalink / raw
  To: help-gnu-emacs

Cecil Westerhof <Cecil@decebal.nl> writes:

> David Kastrup <dak@gnu.org> writes:
>
>>>>> At the moment I create a list with:
>>>>>       (setq ret-val (cons total-amount (cons current-amount ())))
>>>>> I thought about doing it with:
>>>>>       (setq ret-val (cons total-amount '(current-amount)))
>>>>>
>>>>> But then the second value is the string current-amount
>>>>
>>>> Wrong.  The _symbol_ current-amount.
>>>
>>> When evaluating I got:
>>>     (1570378.2570192777 current-amount)
>>> That is why I thought I got the string.
>>
>> A string would have quote marks around it.
>
> Off course.
>
> But I have a few questions on my mind.
> - Why is the first shown as value and the second as _symbol_?

Both floating point numbers and symbols ARE VALUES!

The first object is a floating point number, the second object is a
symbol.  Therefore it shows a floating point number and a symbol.


The rules of evaluation of lisp include:

(defun eval (expression)
   (cond
     ((symbolp expression)  (symbol-value expression)) ; symbols evaluate to their value
     ((atom expression)     expression))               ; atoms are self evaluating
     (t  (case (first expression)
           ((quote)  (second expression))              ; (quote x) returns x unchanged.
           ; ... other special operators
           (else
              (cond
                 ((and (listp (first expression))
                       (eq 'lambda (first (first expression))))
                    ;; ... process ((lambda (...) ...) ...)
                    )
                 ((macro-function (first expression))
                    ;; ... expand the macro call
                    )
                 ((and (symbolp (first expression)) (fboundp (first expression)))
                   (apply (symbol-function (first expression))
                          (mapcar (function eval) (rest expression)) ; evaluate the arguments
                          )))))))


When you evaluate  (cons current-amount ()), since cons is a symbol
that is fbound, the arguments are evaluated:
    since current-amount is a symbol, its symbol-value is returned.
    since () is a symbol, its symbol-value is returned (it's nil).
    the function  #<subr cons> is called with the above values.

When you evalute (quote (current-value)), the list (current-value) is
returned. The evaluation doesn't even look at what's inside the list!



> - current-amount is a local variable. How is it possible that the symbol
>   is still defined?

You exist.  How is it possible your name still exists?


-- 
__Pascal Bourguignon__


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

* Re: Creating a list
  2009-11-19 13:47         ` Barry Margolin
@ 2009-11-19 15:11           ` Cecil Westerhof
  2009-11-19 19:26             ` Pascal J. Bourguignon
  0 siblings, 1 reply; 12+ messages in thread
From: Cecil Westerhof @ 2009-11-19 15:11 UTC (permalink / raw
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

>> Off course.
>
> I think you mean "of course".

English is not my first language. I'll try to be more correct.


>> But I have a few questions on my mind.
>> - Why is the first shown as value and the second as _symbol_?
>
> Because you quoted the list containing the symbol, so it wasn't 
> evaluated.

Okay, thanks. At the moment it is not important, because I now work with
list instead off with cons, but how would I get it evaluated?


>> - current-amount is a local variable. How is it possible that the symbol
>>   is still defined?
>
> Symbols are permanent, only bindings are local.

Thanks again.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


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

* Re: Creating a list
  2009-11-19 13:57         ` David Kastrup
@ 2009-11-19 15:14           ` Cecil Westerhof
  0 siblings, 0 replies; 12+ messages in thread
From: Cecil Westerhof @ 2009-11-19 15:14 UTC (permalink / raw
  To: help-gnu-emacs

David Kastrup <dak@gnu.org> writes:

> I really recommend that you read a Lisp introduction.  Your questions
> are very very basic.

I'll do that. I downloaded and printed some documentation.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


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

* Re: Creating a list
  2009-11-19 15:11           ` Cecil Westerhof
@ 2009-11-19 19:26             ` Pascal J. Bourguignon
  0 siblings, 0 replies; 12+ messages in thread
From: Pascal J. Bourguignon @ 2009-11-19 19:26 UTC (permalink / raw
  To: help-gnu-emacs

Cecil Westerhof <Cecil@decebal.nl> writes:

> Barry Margolin <barmar@alum.mit.edu> writes:
>
>>> Off course.
>>
>> I think you mean "of course".
>
> English is not my first language. I'll try to be more correct.
>
>
>>> But I have a few questions on my mind.
>>> - Why is the first shown as value and the second as _symbol_?
>>
>> Because you quoted the list containing the symbol, so it wasn't 
>> evaluated.
>
> Okay, thanks. At the moment it is not important, because I now work with
> list instead off with cons,
               of


Lisp lists are implemented with cons cells.  So when you work with
lists, in lisp, you do work with conses at the same time.  This is an
important thing to understand about lisp.

While you can develop in lisp abstract data type, where the
implementation details are hidden, and where you provide all the
functional abstraction needed, in the case of conses, and lists and
trees and any other data structure built upon them, the implementation
is left leaking, so you can pun, you can play word games with these data.


Cons level functions are: cons car cdr consp (and atom).

Symbol level functions are: null

List level functions are: list list* first second ... tenth rest endp listp.

When you write:

 (let ((la (list 1 2 3)))
    (let ((lb (cons 0 la)))
       lb))

you are punning on the fact that a lisp list is a chain of conses or
the symbol nil, and that a reference to a list is also a reference to
the first cons of the list (or nil when it's empty).   

To be consistent, you should actually write:

(require 'cl)
(let ((la (list 1 2 3)))
    (let ((lb (list* 0 la)))
       lb))


Similarly, you shouldn't write (null list), but (endp list).  Notice
that endp doesn't behave exactly like null, and that in general, when
you are processing lists, you want the meaning of endp, not that of
null.


So while it may be important for the human reader to use the functions
corresponding to the right level of the data type considered, it is
also nice to be able to change of level from time to time, and be able
to write (cddr tree) to get the children of the node tree, if we
implement these trees as lists containing the label, the color, and
the children.  Only you don't use cddr all over the program, you wrap
it in a tree-children function:

(defun make-tree (label color children)
  (list* label color children)) ; list function
(defun tree-children (tree)
  (cddr tree)) ; pun: cons function, we're considering the 
               ;      list as the sequence of conses it is.



> but how would I get it evaluated?

You can get a symbol, or any expression, evaluated by not doing what
you do to prevent it being evaluated.

Let's read again the question and the answer:

>>> - Why is the first shown as value and the second as _symbol_?
>>
>> Because you quoted the list containing the symbol, so it wasn't 
>> evaluated.


So when you quote an object it doesn't get evaluated.  How can you get
it evaluated?  By NOT quoting it.

Just try it.  Type M-x ielm RET

Then type each expression followed by a RET in the *ielm* buffer: 

(defvar current 42)
'current
current
(quote current)
current
(defun furrent () (* 21 2))
'(furrent)
(furrent)
(quote (furrent))
(furrent)


-- 
__Pascal Bourguignon__


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

end of thread, other threads:[~2009-11-19 19:26 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-19  7:38 Creating a list Cecil Westerhof
2009-11-19  8:35 ` David Kastrup
2009-11-19 10:59   ` Cecil Westerhof
2009-11-19 11:41     ` David Kastrup
2009-11-19 13:13       ` Cecil Westerhof
2009-11-19 13:47         ` Barry Margolin
2009-11-19 15:11           ` Cecil Westerhof
2009-11-19 19:26             ` Pascal J. Bourguignon
2009-11-19 13:57         ` David Kastrup
2009-11-19 15:14           ` Cecil Westerhof
2009-11-19 13:59         ` Pascal J. Bourguignon
2009-11-19 11:59   ` Gnus: Some new mails are automatically marked as 'O' Wang Lei

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.