From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: Elisp function that performs numeric computations Date: Wed, 19 Jan 2022 11:14:27 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="qhImUW+d4rlKDOrD" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="336"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 19 11:15:41 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 1nA80C-000AX0-T9 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 19 Jan 2022 11:15:40 +0100 Original-Received: from localhost ([::1]:42480 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nA80B-0006CD-St for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 19 Jan 2022 05:15:39 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:57768) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nA7z9-00069t-FX for help-gnu-emacs@gnu.org; Wed, 19 Jan 2022 05:14:36 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]:51138) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nA7z6-0006Ka-HK for help-gnu-emacs@gnu.org; Wed, 19 Jan 2022 05:14:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=From:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:To:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=/4qSS475ey/XYrhaAGc98HdkncOXub2kFLEz0IgxBlU=; b=DYrCgv6Dvib9ofPlAa6Mkub0AX xFSLLRJhXvDvQGkZUW40RvIp3XehERvCUyA2/wqujN0OspYU3EDLWqngbogWMNjvnhWFUE6lVxX/T JVVOFzDdpgffhD0vwA35TWqiACgGFhyAhqdbTz5d1wMkavTWtYyGhG4nxndnRNPfkPu1eBdJTeIh5 rkkUXTnlUU5wne67ToPj/bz+Ljj7xqXPyfMVMR9Pa+Tw1i5FuItl/kvI1k8p8uZWagvHLAPXylwh8 kQGdZqzIHLB+CBCFcWm8D43gVOrDTUPcNEmvC3+cGxMuC2GYBeiNH3GfDvw5Ra4unqlf6Y7LHDTBQ Ji7kbDIw==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.94.2) (envelope-from ) id 1nA7z1-0008Kx-0O for help-gnu-emacs@gnu.org; Wed, 19 Jan 2022 11:14:27 +0100 Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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:135441 Archived-At: --qhImUW+d4rlKDOrD Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jan 19, 2022 at 10:03:28AM +0100, fatiparty--- via Users list for t= he GNU Emacs text editor wrote: > Jan 19, 2022, 19:20 by help-gnu-emacs@gnu.org: >=20 > > > > I would like to construct an elisp function that performs numeric compu= tations and outputs the result. > > > > j =3D rptdepth > > w =3D maxdepth - j > > p =3D w + 1 > > > > output =3D j + ( (depth - maxdepth - 1) mod p ) > > > I have done as follows.=C2=A0 But one problem is: How do I use the output= in another elisp function? >=20 > (defun test (depth maxdepth rptdepth) > =C2=A0 "Compute depth to use." > =C2=A0 (interactive) > =C2=A0=20 > =C2=A0 (let* ( (j rptdepth) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (w (- maxdepth j)) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (p (+ w 1)) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (r (mod (- depth m= axdepth 1) p) ) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (o (+ j r)) ) >=20 > =C2=A0=C2=A0=C2=A0 (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) =C2=A0 "Compute depth to use." =C2=A0 (interactive) =C2=A0=20 =C2=A0 (let* ( (j rptdepth) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (w (- maxdepth j)) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (p (+ w 1)) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (r (mod (- depth m= axdepth 1) p) ) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (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)) =2E.. 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) =3D=3D (- maxdepth rptdepth) p <- (+ w 1) =3D=3D (+ (- maxdepth rptdepth) 1) r <- (mod (- depth maxdepth 1) p) =3D=3D (mod (- depth maxdepth 1) (+ (- maxdepth rptdepth))) o <- (+ j r) =3D=3D (+ rptdepth (mod (- depth maxdepth 1) (+ (- maxdepth rptdepth) 1))) =2E..so your function might look like (defun test (depth maxdepth rptdepth) =C2=A0 "Compute depth to use." =C2=A0 (interactive) =C2=A0=20 (+ 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 --qhImUW+d4rlKDOrD Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCYefkfAAKCRAFyCz1etHa RqDBAJ4urXngfyV60a289ULJfjPVjV+dDQCffYsdUJXqK6A1xsIRIDy2s3B9/wU= =YnG+ -----END PGP SIGNATURE----- --qhImUW+d4rlKDOrD--