help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating a list


From: Cecil Westerhof
Subject: Re: Creating a list
Date: Thu, 19 Nov 2009 11:59:19 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (gnu/linux)

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]