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

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

Re: Elisp function that performs numeric computations


From: Marcin Borkowski
Subject: Re: Elisp function that performs numeric computations
Date: Wed, 19 Jan 2022 12:29:15 +0100
User-agent: mu4e 1.1.0; emacs 28.0.50

On 2022-01-19, at 11:14, tomas@tuxteam.de wrote:

> On Wed, Jan 19, 2022 at 10:03:28AM +0100, fatiparty--- via Users list for the 
> GNU Emacs text editor wrote:
>> Jan 19, 2022, 19:20 by help-gnu-emacs@gnu.org:
>> 
>> >
>> > I would like to construct an elisp function that performs numeric 
>> > computations and outputs the result.
>> >
>> > j = rptdepth
>> > w = maxdepth - j
>> > p = w + 1
>> >
>> > output = j + ( (depth - maxdepth - 1) mod p )
>> >
>> I have done as follows. But one problem is: How do I use the output in 
>> another elisp function?
>> 
>> (defun test (depth maxdepth rptdepth)
>>  "Compute depth to use."
>>  (interactive)
>>  
>>  (let* ( (j rptdepth)
>>  (w (- maxdepth j))
>>  (p (+ w 1))
>>  (r (mod (- depth maxdepth 1) p) )
>>  (o (+ j r)) )
>> 
>>  (message "usedepth: %d" o) ))
>
> Exactly the same way the other functions you are using in there do it
> "minus" (aka "-"), "plus" ("+") "mod" and the others).
>
> I seriously recommend you go through the excellent "Emacs Lisp Intro"
> delivered with your documentation.
>
> When you define a function, its "value" (i.e. the result of evaluating
> an expression where this function is the operator) is the last
> expression evaluated in that function. So, to put a very simple example
> (the following function always evaluates to 42);
>
>   (defun forty-two ()
>     42)
>
> That's it.
>
> In your case, see "transformation 1".
>
>   (defun test (depth maxdepth rptdepth)
>    "Compute depth to use."
>    (interactive)
>  
>    (let* ( (j rptdepth)
>    (w (- maxdepth j))
>    (p (+ w 1))
>    (r (mod (- depth maxdepth 1) p) )
>    (o (+ j r)) )
>       o))
>
> (Note that I just say "o" instead of "message ...": the former says it
> "to the program", the latter "to the user".
>
> You then can invoke your function in your program, like so:
>
>   (message "the test is: %d" (test 20 22 19))
>
> ... the expression (test 20 22 19) evaluating to whatever you programmed
> your function to do.

Also, once you are comfortable with the basic building blocks of Elisp,
you'll be able to write a function which returns a value when called
from Elisp and prints it (with `message') when called interactively.
(See 
https://www.gnu.org/software/emacs/manual/html_node/elisp/Distinguish-Interactive.html)

Best,

-- 
Marcin Borkowski
http://mbork.pl



reply via email to

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