From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Elisp function that performs numeric computations Date: Wed, 19 Jan 2022 12:29:15 +0100 Message-ID: <87czkoezg4.fsf@mbork.pl> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18810"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.1.0; emacs 28.0.50 Cc: help-gnu-emacs@gnu.org To: tomas@tuxteam.de Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 19 12:53:23 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nA9Wl-0004je-2G for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 19 Jan 2022 12:53:23 +0100 Original-Received: from localhost ([::1]:46988 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nA9Wj-0005kR-N1 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 19 Jan 2022 06:53:21 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:55768) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nA99y-0001VS-LO for help-gnu-emacs@gnu.org; Wed, 19 Jan 2022 06:29:53 -0500 Original-Received: from mail.mojserwer.eu ([195.110.48.8]:51836) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nA99a-00049Z-B5 for help-gnu-emacs@gnu.org; Wed, 19 Jan 2022 06:29:30 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 5C8D9E6838; Wed, 19 Jan 2022 12:29:20 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kg0ZSvYIMs0G; Wed, 19 Jan 2022 12:29:16 +0100 (CET) Original-Received: from localhost (83.8.36.170.ipv4.supernova.orange.pl [83.8.36.170]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 1F729E6177; Wed, 19 Jan 2022 12:29:16 +0100 (CET) In-reply-to: Received-SPF: pass client-ip=195.110.48.8; envelope-from=mbork@mbork.pl; helo=mail.mojserwer.eu X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:135443 Archived-At: 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