unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Marcin Borkowski <mbork@mbork.pl>
To: tomas@tuxteam.de
Cc: help-gnu-emacs@gnu.org
Subject: Re: Elisp function that performs numeric computations
Date: Wed, 19 Jan 2022 12:29:15 +0100	[thread overview]
Message-ID: <87czkoezg4.fsf@mbork.pl> (raw)
In-Reply-To: <Yefkg5nYYYOnvZ/j@tuxteam.de>


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	other threads:[~2022-01-19 11:29 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-19  7:20 Elisp function that performs numeric computations fatiparty--- via Users list for the GNU Emacs text editor
     [not found] ` <MtlCLWs--3-2@tutanota.com-MtlItBn----2>
2022-01-19  9:03   ` fatiparty--- via Users list for the GNU Emacs text editor
2022-01-19 10:14     ` tomas
2022-01-19 11:29       ` Marcin Borkowski [this message]
2022-01-19 22:52         ` fatiparty--- via Users list for the GNU Emacs text editor
2022-01-20 22:53           ` Sergey Organov
2022-01-21  8:23             ` fatiparty--- via Users list for the GNU Emacs text editor
2022-01-21 11:39               ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-01-21 13:03                 ` Sergey Organov
2022-01-21 15:32                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-01-21 17:13                     ` Sergey Organov
2022-01-21  9:04             ` fatiparty--- via Users list for the GNU Emacs text editor
2022-01-21 11:51               ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-01-21 12:25                 ` fatiparty--- via Users list for the GNU Emacs text editor
     [not found]         ` <MtoXj5p--3-2@tutanota.com-MtoXrZH----2>
2022-01-20 18:17           ` fatiparty--- via Users list for the GNU Emacs text editor
2022-01-19 10:24     ` Manuel Giraud
2022-01-19 17:31     ` Eduardo Ochs
2022-01-19 15:31 ` Michael Heerdegen
2022-01-19 18:41   ` H. Dieter Wilhelm

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87czkoezg4.fsf@mbork.pl \
    --to=mbork@mbork.pl \
    --cc=help-gnu-emacs@gnu.org \
    --cc=tomas@tuxteam.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).