From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Arne Babenhauserheide Newsgroups: gmane.lisp.guile.devel Subject: Re: I wrote fluid advection code: How to make this more elegant? Date: Sun, 24 Jan 2016 16:04:40 +0100 Message-ID: <877fiznhmf.fsf@web.de> References: <87io2kpqdq.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" X-Trace: ger.gmane.org 1453647914 7902 80.91.229.3 (24 Jan 2016 15:05:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 24 Jan 2016 15:05:14 +0000 (UTC) Cc: Arne Babenhauserheide , guile-devel To: Panicz Maciej Godek Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sun Jan 24 16:05:05 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 1aNMDr-0004ar-TV for guile-devel@m.gmane.org; Sun, 24 Jan 2016 16:05:00 +0100 Original-Received: from localhost ([::1]:60764 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNMDq-0004Bb-SO for guile-devel@m.gmane.org; Sun, 24 Jan 2016 10:04:58 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51687) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNMDj-0004BV-Cm for guile-devel@gnu.org; Sun, 24 Jan 2016 10:04:52 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aNMDf-0007Te-B4 for guile-devel@gnu.org; Sun, 24 Jan 2016 10:04:51 -0500 Original-Received: from mout.web.de ([212.227.17.12]:49342) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNMDf-0007TR-2N for guile-devel@gnu.org; Sun, 24 Jan 2016 10:04:47 -0500 Original-Received: from fluss ([85.212.92.95]) by smtp.web.de (mrweb103) with ESMTPSA (Nemesis) id 0MQNma-1aX9LE0iFQ-00Tlct; Sun, 24 Jan 2016 16:04:44 +0100 User-agent: mu4e 0.9.15; emacs 24.5.1 In-reply-to: X-Provags-ID: V03:K0:4pufuiV7veTBRav2JKe2hTAEmHDnkhkJY25grEvR2Kyz0M+JJ/N JYqYDpvFgctyQaMLzqGqwaqLVWumrzA1JOCzSp1IjyFPKh0nB+N3fYr4ZJ7T3KQWonEOBlr +i5+IGazTRvD5rAPS22UoOXzuJg3cwyAt9Kd1rAtyjZjF7jg8c1hljEPEsxPANumabQN9km 7NZZ2QzHHyEJR43VXK1wA== X-UI-Out-Filterresults: notjunk:1;V01:K0:Hq3Xxn0KBGI=:jAoFP/TM792QlnafRfv0uM dTmxYPstc/RjvERKZP2m3Kpzy4lSWET/2V3tgnGFFHhCPtRHn+Dc8Rkn0pueCm73Rpfrbm77c e8UaBMDEPbZPiwjW0ygMwpcHgUAHlNe1z8RfO078D2nHNHPpzJsuSpxxG9yZTM9O4GUda4iDg QCag4lWs/6Po9/W4CiCkcLu2p9n8xgL9NfBm93wRHKuVGk6FIhkFlzClOCl58+BMQ5P+15brE VpYnWgDn3UBVvSw9hj1ZVLRXZA1du1+qW6lL4yJwPmu14jnuY7gG/S8EQGafs+zUInw5EoQR9 QDeoNmS/Ef8LWKv2lz8eNir2bxJNuvE3pe3CtczTuuV5S+hqhgUm1+wGFTw0I5gucMVc+qWQB 2LOc0sP22xVKr9zCxAovv+m4wiIYsegqmq01PTJr+GAt+QFiXURWfbEKyqaqbsrQKBi8HDqYr JjrglRQCG7BqBhErDi3JMZD6IKtVtAuqAtGPRitqISRb85SsuCut5ecgozs3HLpxR7fIPU8kf B82mr1zOlmv039HS+rAQdMzGYVZGww8324Z8kk4aFixH77n/r3LDEbV7U+vTvCmGT2dk4oom6 QsItRWUZGOxE5vqLA7CMsqiIq43MtGyKFfB4/Pj+YgvRpDGSJkrCAd7G5+PTImwtqRvBRcq1M S9etraSpxYYmaj83xCV6mto9pT3WuaOsqJiK9mTdCWLi9w4GJg42b5QfCEKMdW6RXQLv37w1u D0UQB4TMnoM7n8SCW786C3ysIuOKYr03WGxASeNsraq4iJ+SahgLwtW/LvY= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.17.12 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:18131 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Panicz Maciej Godek writes: > although I cannot be of immediate help with the topic, because I don't kn= ow > anything about advection, I think that the main problem with your code is > that it is imperative. While Python's stylistics handles the imperative > code quite nicely, it looks rather strange in Scheme. > > From my experience, the proper Scheme solution should resemble the > mathematical formulation of a problem (except that it should be more > descriptive), rather than a list of steps for solving it. That=E2=80=99s a good point =E2=80=94 I should have given the mathematical = formulation along with the code. The advection equation this implements is: (=CF=86_j,n+1 - =CF=86_j,n)/=CE=94t + c(=CF=86_j+1,n - =CF=86_j-1,n)/2=CE=94x - (c=C2=B2 =CE=94t/=CE=94x) * (=CF=86_j+1,n - 2=CF=86_j,n + =CF=86_j-1,n)= / 2=CE=94x =3D 0 [sadly this isn=E2=80=99t easy to read, either, but it=E2=80=99s the math w= hich needs implementation] > Also, it should be more readable to use pattern matching instead of list > indexing, so most likely your expression > > (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)) > > should look like this > > (map (lambda (prev this next) > (- this > (* c1 (- next prev)) > (* (- c2) (+ next (* -2 this) prev)))) > `(0 ,@(drop psir 1)) > psir > `(,@(drop-right psir 1) 0)) > > While this may also look slightly difficult to read (and write), I think it already helps a lot, that this is shorter (prev this next clearly wins against (+/- i 1)). > this isn't solely because of the expression's structure, but because > the factors of the expression have no name, and therefore the source > code doesn't explain their role (this is the problem of the Python > code either, but Python doesn't prompt to fix that) that part is already in the math, but using variables with better names sounds like an improvement. Thank you! > PS I think that this subject fits better to guile-user You=E2=80=99re right =E2=80=94 sorry. I=E2=80=99m still spoiled by projects= where exactly one mailing list is active=E2=80=A6 Best wishes, Arne =2D-=20 Unpolitisch sein hei=C3=9Ft politisch sein ohne es zu merken --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iJwEAQEIAAYFAlak6AsACgkQ3M8NswvBBUi3pgP+LiOM1QNal/Dgvo/Qicfqdm0j Srnf2gIulyI+t4s18zbgOOyN/15ZeICvta6aAXOhxJ7zWJTuU2M06e7Ge4ws8OxM mTwzy1DP96zq4f3hVmogQiI5UE8OHutj+8ez3gawjZjULGJTVI+2Y8RGFMSkzNPm 7ZgPld9FcT/dEV/dFs8= =6Gtc -----END PGP SIGNATURE----- --=-=-=--