From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.lisp.guile.devel Subject: Re: I wrote fluid advection code: How to make this more elegant? Date: Sat, 23 Jan 2016 13:42:27 +0100 Message-ID: <877fj0lb64.fsf@T420.taylan> References: <87io2kpqdq.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1453552674 23051 80.91.229.3 (23 Jan 2016 12:37:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 23 Jan 2016 12:37:54 +0000 (UTC) Cc: guile-devel@gnu.org To: Arne Babenhauserheide Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Jan 23 13:37:53 2016 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aMxRx-0006TB-Hi for guile-devel@m.gmane.org; Sat, 23 Jan 2016 13:37:53 +0100 Original-Received: from localhost ([::1]:57053 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMxRw-0005io-NZ for guile-devel@m.gmane.org; Sat, 23 Jan 2016 07:37:52 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50603) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMxRq-0005gG-N7 for guile-devel@gnu.org; Sat, 23 Jan 2016 07:37:47 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aMxRp-0003UT-OQ for guile-devel@gnu.org; Sat, 23 Jan 2016 07:37:46 -0500 Original-Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]:37926) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aMxRp-0003UD-I2 for guile-devel@gnu.org; Sat, 23 Jan 2016 07:37:45 -0500 Original-Received: by mail-wm0-x230.google.com with SMTP id b14so17934230wmb.1 for ; Sat, 23 Jan 2016 04:37:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type:content-transfer-encoding; bh=8ayhYqt572rriYxKe9huwIB/qEALVtxdqwTBjtzukUY=; b=qyv0M87ak182V8pxIH1CXp2jCUlpX/eVNJ+ADbqz1MM90oCH+HlJrTnVs5rRcWegS4 6tUHN7DzaDMlXnFzuLC6S9/78hQCSDxf4OfBn2ESFFFz2z+sLSiX1rNU8MbZgckz3GiK KQ59d3TajN7oncoyvRSIxz0AiG3TmkESP+7mU2v9XszY6EknFROWkKeqirbhwfZl3iLw +F8sraJfMLo/3CUEtWOUVCB6q4NOVOSmet6ZBBTeUg6Xe603HqF75MaB6I9vmQbJsAme ciR9HdqF+0+ETkhkp5J5a+o7wwAURGapOxpzc0OfE/qZLhr5zBjTAPCTLfpBaWMlE0Xh cuYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type :content-transfer-encoding; bh=8ayhYqt572rriYxKe9huwIB/qEALVtxdqwTBjtzukUY=; b=JDAKH4hHAcSXdAriuP8iCAnzhEzrUtaNDdMSRNBbvuYAp+zUwzKdKw/DQu59VUnqSE 94Wl8GbQPpue3t9D3y0xXo9iCiPdTty9Z3w2KqUHs9rTIpbMOPpmBQRDamtW7jMXwYZZ URL8fqGVuQknHgL5oqnnIDW0tvz0II0mf2UaXw+J81AQdga0SRYcZHzUzKU4ElUFt8A2 pB32efKghDpArlWjCKPSZnbfBg7YclmNyVxBQ4Yovt09vsGlQB/qRwxZZR1lHHV6I6nH 1uoualadIWdXfviqSWQ6szaAAGVtCycc/XuV/3z8gLzzaiRBhRfXvwU63H4z61+IF9jY GUwg== X-Gm-Message-State: AG10YOTZuApkLTfNFSQQV/gqnZ0nGyJbQN2+krhtFDy3kWf8bXpA6oBwWACIXSF8J7YfZA== X-Received: by 10.194.24.195 with SMTP id w3mr2730811wjf.106.1453552664507; Sat, 23 Jan 2016 04:37:44 -0800 (PST) Original-Received: from T420.taylan ([2a02:908:c30:3ec0:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id s8sm10110732wje.35.2016.01.23.04.37.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 23 Jan 2016 04:37:42 -0800 (PST) In-Reply-To: <87io2kpqdq.fsf@web.de> (Arne Babenhauserheide's message of "Sat, 23 Jan 2016 11:00:17 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::230 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:18129 Archived-At: Arne Babenhauserheide writes: > Hi, > > I just recreated a fluid advection exercise in Guile Scheme and I=E2=80= =99m not > quite happy with its readability. Can you help me improve it? > > My main gripe is that the math does not look instantly accessible. > > The original version was in Python: > > psi[i] - c1*(psi[i+1] - psi[i-1]) + c2*(psi[i+1] - 2.0*psi[i] + psi[i= -1]) > > My port to Scheme looks like this: > > (let ((newvalue (+ (- (psir i) > (* c1 (- (psir (+ i 1)) (psir (- i 1))))) > (* c2 (+ (- (psir (+ i 1)) (* 2 (psir i))) > (psir (- i 1))))))) > (array-set! psinew newvalue i)) Guile supports SRFI-105, so that could be: {{psi[i] - {c1 * {psi[{i + 1}] - psi[{i - 1}]}}} + {c2 * {{psi[{i + 1}]= - {2 * psi[i]}} + psi[{i - 1}]}}} which is less readable than the Python version because there's no first-hand support for operator precedence so we {} group all binary operations, plus we need to use {} within [], plus we must separate operators with whitespace, but maybe it's acceptable. You can put "#!curly-infix" at the start of your Scheme file to enable SRFI-105 syntax. Note that all those 'psi[x]' expressions will expand to ($bracket-apply$ psi x) and you can implement a macro of that name to turn that into whatever is suitable at compile-time. (If performance is not an issue, SRFI-123 provides a run-time polymorphic procedure for $bracket-access$.) With a smart definition of $bracket-apply$, you could also cut down those psi[{i + 1}] to psi[i + 1]. The latter will expand to ($bracket-apply$ psi i + 1) which could be accommodated for in a special-purpose definition of $bracket-apply$ such as: (syntax-rules () ((_ obj index) (obj index)) ((_ obj x operator y) (obj (operator x y)))) That would *not* support e.g. psi[i + j - 1], which would instead need to be written e.g. psi[{i + j} - 1], i.e. we only save one pair of {}, but maybe that's good enough. By the way, e.g. {x - y + z} will turn into ($nfx$ x - y + z), and maybe there's a definition for $nfx$ out in the wild (or you can create one) that does operator precedence. (That could then also be used in $bracket-apply$.) So optimally, the whole thing could become: {psi[i] - c1 * {psi[i + 1] - psi[i - 1]} + c2 * {psi[i + 1] - 2 * psi[i= ] + psi[i - 1]}} Taylan