[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Elisp function that performs numeric computations
From: |
tomas |
Subject: |
Re: Elisp function that performs numeric computations |
Date: |
Wed, 19 Jan 2022 11:14:27 +0100 |
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.
Now one could argue that you can simplify your function's innards a bit:
note that you don't make any use of all those intermediate results (j
and so on). So if there isn't a particular reason to name (or keep)
them, you might do.
j <- rptdepth
w <- (- maxdepth j) == (- maxdepth rptdepth)
p <- (+ w 1) == (+ (- maxdepth rptdepth) 1)
r <- (mod (- depth maxdepth 1) p) ==
(mod (- depth maxdepth 1) (+ (- maxdepth rptdepth)))
o <- (+ j r) ==
(+ rptdepth
(mod (- depth maxdepth 1) (+ (- maxdepth rptdepth) 1)))
...so your function might look like
(defun test (depth maxdepth rptdepth)
"Compute depth to use."
(interactive)
(+ rptdepth
(mod (- depth maxdepth 1)
(+ (- maxdepth rptdepth) 1))))
Pick your choice. Further embellishments possible if you remember that
(+ x 1) can be abbreviated as (1+ x) and likewise (- x 1) as (1- x).
Again: go through "Emacs Lisp Intro". I have the strong impression that
you haven't yet a working model of how Lisp ticks.
Cheers
-- t
signature.asc
Description: PGP signature
- Elisp function that performs numeric computations, fatiparty, 2022/01/19
- Message not available
- Elisp function that performs numeric computations, fatiparty, 2022/01/19
- Re: Elisp function that performs numeric computations,
tomas <=
- Re: Elisp function that performs numeric computations, Marcin Borkowski, 2022/01/19
- Elisp function that performs numeric computations, fatiparty, 2022/01/19
- Re: Elisp function that performs numeric computations, Sergey Organov, 2022/01/20
- Elisp function that performs numeric computations, fatiparty, 2022/01/21
- Re: Elisp function that performs numeric computations, Emanuel Berg, 2022/01/21
- Re: Elisp function that performs numeric computations, Sergey Organov, 2022/01/21
- Re: Elisp function that performs numeric computations, Emanuel Berg, 2022/01/21
- Re: Elisp function that performs numeric computations, Sergey Organov, 2022/01/21
- Elisp function that performs numeric computations, fatiparty, 2022/01/21
- Re: Elisp function that performs numeric computations, Emanuel Berg, 2022/01/21