all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Elisp function that performs numeric computations
@ 2022-01-19  7:20 fatiparty--- via Users list for the GNU Emacs text editor
       [not found] ` <MtlCLWs--3-2@tutanota.com-MtlItBn----2>
  2022-01-19 15:31 ` Michael Heerdegen
  0 siblings, 2 replies; 19+ messages in thread
From: fatiparty--- via Users list for the GNU Emacs text editor @ 2022-01-19  7:20 UTC (permalink / raw)
  To: Help Gnu Emacs


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 )




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

* Elisp function that performs numeric computations
       [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
                       ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: fatiparty--- via Users list for the GNU Emacs text editor @ 2022-01-19  9:03 UTC (permalink / raw)
  To: Fatiparty; +Cc: Help Gnu Emacs

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) ))




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

* Re: Elisp function that performs numeric computations
  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
  2022-01-19 10:24     ` Manuel Giraud
  2022-01-19 17:31     ` Eduardo Ochs
  2 siblings, 1 reply; 19+ messages in thread
From: tomas @ 2022-01-19 10:14 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 3133 bytes --]

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: Elisp function that performs numeric computations
  2022-01-19  9:03   ` fatiparty--- via Users list for the GNU Emacs text editor
  2022-01-19 10:14     ` tomas
@ 2022-01-19 10:24     ` Manuel Giraud
  2022-01-19 17:31     ` Eduardo Ochs
  2 siblings, 0 replies; 19+ messages in thread
From: Manuel Giraud @ 2022-01-19 10:24 UTC (permalink / raw)
  To: fatiparty--- via Users list for the GNU Emacs text editor; +Cc: Fatiparty

fatiparty--- via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> 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) ))

Hi,

A `let' form returns the value of the last expression of its body. In
your function, it is "(message…)" which returns the string of the
message. If you want your function to return `o' just append it as the
last expression:

(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)
    o))

Then, you can use `test' directly:

(* (test 8 1000 9) (test 8 1000 9))
-- 
Manuel Giraud



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

* Re: Elisp function that performs numeric computations
  2022-01-19 10:14     ` tomas
@ 2022-01-19 11:29       ` Marcin Borkowski
  2022-01-19 22:52         ` fatiparty--- via Users list for the GNU Emacs text editor
       [not found]         ` <MtoXj5p--3-2@tutanota.com-MtoXrZH----2>
  0 siblings, 2 replies; 19+ messages in thread
From: Marcin Borkowski @ 2022-01-19 11:29 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs


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



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

* Re: Elisp function that performs numeric computations
  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 15:31 ` Michael Heerdegen
  2022-01-19 18:41   ` H. Dieter Wilhelm
  1 sibling, 1 reply; 19+ messages in thread
From: Michael Heerdegen @ 2022-01-19 15:31 UTC (permalink / raw)
  To: help-gnu-emacs

fatiparty--- via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> I would like to construct an elisp function that performs numeric
> computations and outputs the result.

Unless this is just for learning: have a look at M-x quick-calc.
Requires to specify the input at a prompt.  But it offers much
more than Elisp's mathematical functions.

Michael.




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

* Re: Elisp function that performs numeric computations
  2022-01-19  9:03   ` fatiparty--- via Users list for the GNU Emacs text editor
  2022-01-19 10:14     ` tomas
  2022-01-19 10:24     ` Manuel Giraud
@ 2022-01-19 17:31     ` Eduardo Ochs
  2 siblings, 0 replies; 19+ messages in thread
From: Eduardo Ochs @ 2022-01-19 17:31 UTC (permalink / raw)
  To: fatiparty; +Cc: Help Gnu Emacs

On Wed, 19 Jan 2022 at 06:18, fatiparty--- via Users list for the GNU Emacs
text editor <help-gnu-emacs@gnu.org> wrote:

> I have done as follows.  But one problem is: How do I use the output in
> another elisp function?
>
>
Hi Fatiparty,

the easiest way to test small elisp snippets is to run them by typing
C-e C-x C-e on the last line of the snippet... the C-e will move to
the end of the line, and the C-x C-e will then execute the "sexp
before point" and show the result in the echo area. This is explained
here:

  (info "(emacs)Lisp Eval")
  http://www.gnu.org/software/emacs/manual/html_node/emacs/Lisp-Eval.html

Try this on these sexps:

         (* 2 3)
                 (* 4 5)
      (+ (* 2 3) (* 4 5))
  (progn (* 2 3) (* 4 5) (* 6 7))
  (prog1 (* 2 3) (* 4 5) (* 6 7))
  (prog2 (* 2 3) (* 4 5) (* 6 7))

If you like this way of running sexps then you should take a look at
this tutorial, that is all written in this style - "run each of these
sexps with `C-e C-x C-e' and try to understand what they do"...

  http://angg.twu.net/eev-intros/find-elisp-intro.html

Note that the introduction of that tutorial says:

  Different people prefer different kinds of tutorials.
  Many people love the eintr, but I don't: (find-node "(eintr)Top")
  This tutorial here is what I would have liked to have had access to
  when I started learning Emacs Lisp.

Cheers,
  Eduardo Ochs
  http://angg.twu.net/#eev


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

* Re: Elisp function that performs numeric computations
  2022-01-19 15:31 ` Michael Heerdegen
@ 2022-01-19 18:41   ` H. Dieter Wilhelm
  0 siblings, 0 replies; 19+ messages in thread
From: H. Dieter Wilhelm @ 2022-01-19 18:41 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> fatiparty--- via Users list for the GNU Emacs text editor
> <help-gnu-emacs@gnu.org> writes:
>
>> I would like to construct an elisp function that performs numeric
>> computations and outputs the result.
>
> Unless this is just for learning: have a look at M-x quick-calc.
> Requires to specify the input at a prompt.  But it offers much
> more than Elisp's mathematical functions.

Yeah, Calc is one of the killer features of Emacs.

https://www.gnu.org/software/emacs/refcards/pdf/calccard.pdf

      Dieter
-- 
Best wishes
H. Dieter Wilhelm
Zwingenberg, Germany



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

* Elisp function that performs numeric computations
  2022-01-19 11:29       ` Marcin Borkowski
@ 2022-01-19 22:52         ` fatiparty--- via Users list for the GNU Emacs text editor
  2022-01-20 22:53           ` Sergey Organov
       [not found]         ` <MtoXj5p--3-2@tutanota.com-MtoXrZH----2>
  1 sibling, 1 reply; 19+ messages in thread
From: fatiparty--- via Users list for the GNU Emacs text editor @ 2022-01-19 22:52 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: Tomas, Help Gnu Emacs

Jan 19, 2022, 23:29 by mbork@mbork.pl:

>
>  (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))
>>
I would like to adapt the function a little bit more.   If depth <= maxdepth
I want to set o=depth instead of performing the computation shown for the
variable o.




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

* Elisp function that performs numeric computations
       [not found]         ` <MtoXj5p--3-2@tutanota.com-MtoXrZH----2>
@ 2022-01-20 18:17           ` fatiparty--- via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 19+ messages in thread
From: fatiparty--- via Users list for the GNU Emacs text editor @ 2022-01-20 18:17 UTC (permalink / raw)
  To: Fatiparty; +Cc: Marcin Borkowski, Tomas, Help Gnu Emacs


Jan 20, 2022, 10:52 by help-gnu-emacs@gnu.org:

> Jan 19, 2022, 23:29 by mbork@mbork.pl:
>
>>
>> (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))
>>>
> I would like to adapt the function a little bit more.   If depth <= maxdepth
> I want to set o=depth instead of performing the computation shown for the
> variable o.
>
> I would like to use an if statement to set o to depth.  The current computation will
> go with the else  statement.  How does one go about using the let* construct in the
> else part of the conditional.  Would I need to use a progn for this?
>




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

* Re: Elisp function that performs numeric computations
  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  9:04             ` fatiparty--- via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 19+ messages in thread
From: Sergey Organov @ 2022-01-20 22:53 UTC (permalink / raw)
  To: help-gnu-emacs

fatiparty--- via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> Jan 19, 2022, 23:29 by mbork@mbork.pl:
>
>>
>>  (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))
>>>
> I would like to adapt the function a little bit more.   If depth <= maxdepth
> I want to set o=depth instead of performing the computation shown for the
> variable o.

Did I get it right, I wonder?

(defun test (depth maxdepth rptdepth)
  (if (<= depth maxdepth)
      depth
    (+ rptdepth
       (mod (- depth maxdepth 1)
            (- maxdepth rptdepth -1)))))

Is it how true (E)Lisp'er would write it?

-- Sergey Organov




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

* Elisp function that performs numeric computations
  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  9:04             ` fatiparty--- via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 19+ messages in thread
From: fatiparty--- via Users list for the GNU Emacs text editor @ 2022-01-21  8:23 UTC (permalink / raw)
  To: Sergey Organov; +Cc: Help Gnu Emacs

Jan 21, 2022, 10:53 by sorganov@gmail.com:

> fatiparty--- via Users list for the GNU Emacs text editor
> <help-gnu-emacs@gnu.org> writes:
>
>> Jan 19, 2022, 23:29 by mbork@mbork.pl:
>>
>>>
>>> (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))
>>>>
>> I would like to adapt the function a little bit more.   If depth <= maxdepth
>> I want to set o=depth instead of performing the computation shown for the
>> variable o.
>>
>
> Did I get it right, I wonder?
>
> (defun test (depth maxdepth rptdepth)
>  (if (<= depth maxdepth)
>  depth
>  (+ rptdepth
>  (mod (- depth maxdepth 1)
>  (- maxdepth rptdepth -1)))))
>
> Is it how true (E)Lisp'er would write it?
>
> -- Sergey Organov
>

What you have done is good.  But although you have got to a simple solution, I want
to code the more complicated solution for the case where let* is involved (within which
local variables are defined).





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

* Elisp function that performs numeric computations
  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  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
  1 sibling, 1 reply; 19+ messages in thread
From: fatiparty--- via Users list for the GNU Emacs text editor @ 2022-01-21  9:04 UTC (permalink / raw)
  To: Sergey Organov; +Cc: Help Gnu Emacs

Jan 21, 2022, 10:53 by sorganov@gmail.com:

> fatiparty--- via Users list for the GNU Emacs text editor
> <help-gnu-emacs@gnu.org> writes:
>
>> Jan 19, 2022, 23:29 by mbork@mbork.pl:
>>
>>>
>>> (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))
>>>>
>> I would like to adapt the function a little bit more.   If depth <= maxdepth
>> I want to set o=depth instead of performing the computation shown for the
>> variable o.
>>
>
> Did I get it right, I wonder?
>
> (defun test (depth maxdepth rptdepth)
>  (if (<= depth maxdepth)
>  depth
>  (+ rptdepth
>  (mod (- depth maxdepth 1)
>  (- maxdepth rptdepth -1)))))
>
> Is it how true (E)Lisp'er would write it?
> -- Sergey Organov
>
I have done as follows,  which seems to be working correctly.

(defun test (dcur dmax drpt)
  "Compute brace depth used for brace colouring."
  (interactive)

  (if (<= dcur dmax)

      (let ( (n dcur) )
    (message "dcur <= dmax | Typeface: %d" n)
    n)

    (let* ( (j drpt)
            (w (- dmax j))
            (p (+ w 1))
            (r (mod (- dcur dmax 1) p) )
            (n (+ j r)) )
    (message "dcur > dmax | Typeface: %d" n)
    n) ))







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

* Re: Elisp function that performs numeric computations
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-01-21 11:39 UTC (permalink / raw)
  To: help-gnu-emacs

fatiparty--- via Users list for the GNU Emacs text editor wrote:

>>>>> "Compute depth to use."
>>>>>  (interactive)
>>>>>  
>>>>>  (let* ( (j rptdepth)
>>>>>  (w (- maxdepth j))
>>>>>  (p (+ w 1))
>>>>>  (r (mod (- depth maxdepth 1) p) )
>>>>>  (o (+ j r)) )
>>>>>  o))
>>>>>
>>>
>>> I would like to adapt the function a little bit more. If
>>> depth <= maxdepth I want to set o=depth instead of
>>> performing the computation shown for the variable o.
>>>
>>
>> Did I get it right, I wonder?
>>
>> (defun test (depth maxdepth rptdepth)
>>  (if (<= depth maxdepth)
>>  depth
>>  (+ rptdepth
>>  (mod (- depth maxdepth 1)
>>  (- maxdepth rptdepth -1)))))
>>
>> Is it how true (E)Lisp'er would write it?
>>
>
> What you have done is good. But although you have got to
> a simple solution, I want to code the more complicated
> solution for the case where let* is involved (within which
> local variables are defined).

The reason there is no `let' (or `let*') in his code is
because there is no repitition of computation patterns ...

As for "Is it how true (E)Lisp'er would write it?" I'd write
it, whatever it is, like this:

(defun organov (dep top rep)
  (if (<= dep top)
      dep
    (+ rep
       (mod (1- (- dep top))
            (1+ (- top rep)) ))))

Elispers at work!

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Elisp function that performs numeric computations
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-01-21 11:51 UTC (permalink / raw)
  To: help-gnu-emacs

fatiparty--- via Users list for the GNU Emacs text editor wrote:

> (defun test (dcur dmax drpt)

All programming: Name the function something that etc etc.

Lisp: We don't name formal parameters the assembly language or
C style ... give them short, but if possible complete names
that are easy to read and write. When in doubt, give them
a complete name that communicates what its purpose or
intended use.

> "Compute brace depth used for brace colouring."

Doc string should have the formal parameter's names in BOLD in
the order they come in the the function definition ...

> (interactive)

Assign the formal parameters with argument values

(interactive "ndepth: \nntop: \nnrepeat: ")
                                           ^ eval me

Use English in the prompt string, if the words feel to long
see if they can be shortened in a natural way (here, e.g. dep,
top, and rep) - the reason one avoids "max" is it also appears
in as the function `max' ...

> (let ( (n dcur) )
>   (message "dcur <= dmax | Typeface: %d" n)
> n)

If there is no computation, renaming it ... why do you
do that?

You use it like this:

(let*((side 2)
      (square (expt side 2) )) ...
  ;; use square here
  )

-- 
underground experts united
https://dataswamp.org/~incal




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

* Elisp function that performs numeric computations
  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
  0 siblings, 0 replies; 19+ messages in thread
From: fatiparty--- via Users list for the GNU Emacs text editor @ 2022-01-21 12:25 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: Help Gnu Emacs

Jan 21, 2022, 23:51 by help-gnu-emacs@gnu.org:

> fatiparty--- via Users list for the GNU Emacs text editor wrote:
>
>> (defun test (dcur dmax drpt)
>>
>
> All programming: Name the function something that etc etc.
>
> Lisp: We don't name formal parameters the assembly language or
> C style ... give them short, but if possible complete names
> that are easy to read and write. When in doubt, give them
> a complete name that communicates what its purpose or
> intended use.
>
>> "Compute brace depth used for brace colouring."
>>
>
> Doc string should have the formal parameter's names in BOLD in
> the order they come in the the function definition ...
>
>> (interactive)
>>
>
> Assign the formal parameters with argument values
>
> (interactive "ndepth: \nntop: \nnrepeat: ")
>  ^ eval me
>
> Use English in the prompt string, if the words feel to long
> see if they can be shortened in a natural way (here, e.g. dep,
> top, and rep) - the reason one avoids "max" is it also appears
> in as the function `max' ...
>
>> (let ( (n dcur) )
>>  (message "dcur <= dmax | Typeface: %d" n)
>> n)
>>
>
> If there is no computation, renaming it ... why do you
> do that?
>

I did that because I use n an the output (either it is dcur [current depth] or it is from the
latter computation).

But as you say, I could just do

(if (<= dcur dmax)      dcur

(let* ( (j drpt) ...



> You use it like this:
>
> (let*((side 2)
>  (square (expt side 2) )) ...
>  ;; use square here
>  )
>
> -- 
> underground experts united
> https://dataswamp.org/~incal
>




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

* Re: Elisp function that performs numeric computations
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Sergey Organov @ 2022-01-21 13:03 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> fatiparty--- via Users list for the GNU Emacs text editor wrote:
>
>>>>>> "Compute depth to use."
>>>>>>  (interactive)
>>>>>>  
>>>>>>  (let* ( (j rptdepth)
>>>>>>  (w (- maxdepth j))
>>>>>>  (p (+ w 1))
>>>>>>  (r (mod (- depth maxdepth 1) p) )
>>>>>>  (o (+ j r)) )
>>>>>>  o))
>>>>>>
>>>>
>>>> I would like to adapt the function a little bit more. If
>>>> depth <= maxdepth I want to set o=depth instead of
>>>> performing the computation shown for the variable o.
>>>>
>>>
>>> Did I get it right, I wonder?
>>>
>>> (defun test (depth maxdepth rptdepth)
>>>  (if (<= depth maxdepth)
>>>  depth
>>>  (+ rptdepth
>>>  (mod (- depth maxdepth 1)
>>>  (- maxdepth rptdepth -1)))))
>>>
>>> Is it how true (E)Lisp'er would write it?
>>>
>>
>> What you have done is good. But although you have got to
>> a simple solution, I want to code the more complicated
>> solution for the case where let* is involved (within which
>> local variables are defined).
>
> The reason there is no `let' (or `let*') in his code is
> because there is no repitition of computation patterns ...
>
> As for "Is it how true (E)Lisp'er would write it?" I'd write
> it, whatever it is, like this:
>
> (defun organov (dep top rep)
>   (if (<= dep top)
>       dep
>     (+ rep
>        (mod (1- (- dep top))
>             (1+ (- top rep)) ))))

Nice, thanks! Didn't know me could be cut to such a brief definition
though :)

-- Sergey Organov




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

* Re: Elisp function that performs numeric computations
  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
  0 siblings, 1 reply; 19+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-01-21 15:32 UTC (permalink / raw)
  To: help-gnu-emacs

Sergey Organov wrote:

>> (defun organov (dep top rep)
>>   (if (<= dep top)
>>       dep
>>     (+ rep
>>        (mod (1- (- dep top))
>>             (1+ (- top rep)) ))))
>
> Nice, thanks! Didn't know me could be cut to such a brief
> definition though :)

Heh, but here it actually becomes longer than yours!

But more clear, was the thought.

So if that's indeed so, that means one should do it, since the
penalty for being long (or, if you'd like, the gain from being
short) is approaching zero to the point (but not to the acutal
point actually) where it is infinitely small :)

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Elisp function that performs numeric computations
  2022-01-21 15:32                   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-01-21 17:13                     ` Sergey Organov
  0 siblings, 0 replies; 19+ messages in thread
From: Sergey Organov @ 2022-01-21 17:13 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> Sergey Organov wrote:
>
>>> (defun organov (dep top rep)
>>>   (if (<= dep top)
>>>       dep
>>>     (+ rep
>>>        (mod (1- (- dep top))
>>>             (1+ (- top rep)) ))))
>>
>> Nice, thanks! Didn't know me could be cut to such a brief
>> definition though :)
>
> Heh, but here it actually becomes longer than yours!

Heh, but mine has not been called "organov", so it didn't cut *me*, and
then yours did, and now I'm sitting here feeling being much shorter than
I was only yesterday :)

-- Sergey Organov




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

end of thread, other threads:[~2022-01-21 17:13 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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.