From mboxrd@z Thu Jan 1 00:00:00 1970 From: Efraim Flashner Subject: Re: Adding wc to Bournish Date: Wed, 25 May 2016 13:05:03 +0300 Message-ID: <20160525100503.GB15009@debian-netbook> References: <20160524184720.GA27449@debian-netbook> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="i9LlY+UWpKt15+FH" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:33027) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5Vgm-0002CC-7l for guix-devel@gnu.org; Wed, 25 May 2016 06:05:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5Vgi-0004ro-VW for guix-devel@gnu.org; Wed, 25 May 2016 06:05:20 -0400 Content-Disposition: inline In-Reply-To: List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: Ricardo Wurmus Cc: guix-devel@gnu.org --i9LlY+UWpKt15+FH Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, May 25, 2016 at 11:26:11AM +0200, Ricardo Wurmus wrote: >=20 > Efraim Flashner writes: >=20 > > +(define (wc-l-command file) > > + (let* ((input-file (open-file file "r")) > > + (line (read-line input-file)) > > + (line-count 0)) > > + (while (not (eof-object? line)) > > + (set! line-count (1+ line-count)) > > + (set! line (read-line input-file))) > > + line-count)) >=20 > It=E2=80=99s unusual for me to see the use of =E2=80=9Cwhile=E2=80=9D and= =E2=80=9Cset!=E2=80=9D in Scheme code. > You could do this in a functional manner using a fold (see SRFI-1) or > with file streams (see SRFI-41), which also provides a stream-fold. I'm going to put set! in the GOTO box. GOTO worked well enough when I learned QBasic, but just because its implemented doesn't mean I should still use it. (ie: its there but try not to use it) >=20 > The idea with a fold is that you have a function that takes a value > (e.g. from a list or a stream) and an intermediate result. The function > does something to the value and then returns a new intermediate result. I already like this much better >=20 > Here=E2=80=99s a fold over a list of symbols implementing a count: >=20 > (fold > (lambda (_ res) (+ res 1)) ; increase the result > 0 ; start at 0 > '(hello world bye)) ; items to count >=20 > If you had a file stream, where each element represents one line, you > can fold over all lines in much the same way to get a count. You could > use the same framework with a different stream element generator > (reading one word or byte at a time instead of one line at a time) to > implement the other features of =E2=80=9Cwc=E2=80=9D. I'll take another look at streams and rework that. I still think asking the filesystem how big the file is works well for `wc -c', but `wc -l' and `wc -w' should really be part of the same function. >=20 > There=E2=80=99s an example of how to define a file stream in the Guile ma= nual in > the documentation for SRFI-41. That looks like a good place to start then :) >=20 > ~~ Ricardo --=20 Efraim Flashner =D7=90=D7=A4=D7=A8=D7=99=D7=9D = =D7=A4=D7=9C=D7=A9=D7=A0=D7=A8 GPG key =3D A28B F40C 3E55 1372 662D 14F7 41AA E7DC CA3D 8351 Confidentiality cannot be guaranteed on emails sent or received unencrypted --i9LlY+UWpKt15+FH Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJXRXjLAAoJEPTB05F+rO6TvsYP/R/xghZtWCacj8NLzpM8bdhU 8blAXqlM/SkCcQ3+v5khSOqfj13vX79jB6ZlI/DbeD6VpULnEo/q3piBfrx+FJ/Y YDi2ljpZHqkxjUOCF/diYTqd0fbfgrJqP9Aapafxpv7lql0oEijuZASb7wXhlL+0 5+eqiAZAmNm5UOLfp2u3Tzlms8BBZZRcqV/VPXHo8VnxJrxMkJBezBOY7fmq5CD9 Ppy+1isqy8LB/NUGytViLev/cC1nq5zYLtxjo2x6RYODyiHK0WPcRh+VPKGAPIRA 6PGFRFpwRCCydzP4Hw6Gv5grWKiPU02oReE8GMOtzo9VBSga+30lOTayY7SqounJ NeJPdrUVH/f1T1HQrwbrk8qCotYBN4kgzSQN9dVWX9eofeRyFkUN7GAl1CbtV6Rs +ieo6GiRKxEeq7J1718YjAXYoNRyixJueUGE6EwzxGTd2zFjoBdwokBgSKazIfX7 WRL09HjnAmDh7P/Rof3q6XjNFMsdmsye3Ob/rbzQFJD7A67ErT9igrXdDLtuj3Nl TmAG0B4Ty2x8uDzVbpoXP/CnIuMmpXTFAG4w+Dcku7rVSkYXgYRJTeQbjzTcb5Cl i6ETn68lS3/T4VoGTWL8rurhWpwXdh90PQbVRNfFv9D+Mq9Pxd2mRhlGKi7fAPPe pDIawicpkmcPNkMjb/WD =yLLA -----END PGP SIGNATURE----- --i9LlY+UWpKt15+FH--