unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Efraim Flashner <efraim@flashner.co.il>
To: Ricardo Wurmus <ricardo.wurmus@mdc-berlin.de>
Cc: guix-devel@gnu.org
Subject: Re: Adding wc to Bournish
Date: Wed, 25 May 2016 13:05:03 +0300	[thread overview]
Message-ID: <20160525100503.GB15009@debian-netbook> (raw)
In-Reply-To: <idj1t4q4hfw.fsf@bimsb-sys02.mdc-berlin.net>

[-- Attachment #1: Type: text/plain, Size: 2344 bytes --]

On Wed, May 25, 2016 at 11:26:11AM +0200, Ricardo Wurmus wrote:
> 
> Efraim Flashner <efraim@flashner.co.il> writes:
> 
> > +(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))
> 
> It’s unusual for me to see the use of “while” and “set!” 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)

> 
> 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

> 
> Here’s a fold over a list of symbols implementing a count:
> 
>    (fold
>       (lambda (_ res) (+ res 1))  ; increase the result
>       0                           ; start at 0
>       '(hello world bye))         ; items to count
> 
> 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 “wc”.

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.

> 
> There’s an example of how to define a file stream in the Guile manual in
> the documentation for SRFI-41.

That looks like a good place to start then :)

> 
> ~~ Ricardo

-- 
Efraim Flashner   <efraim@flashner.co.il>   אפרים פלשנר
GPG key = A28B F40C 3E55 1372 662D  14F7 41AA E7DC CA3D 8351
Confidentiality cannot be guaranteed on emails sent or received unencrypted

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2016-05-25 10:05 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-24 18:47 Adding wc to Bournish Efraim Flashner
2016-05-25  9:03 ` Ricardo Wurmus
2016-05-25  9:43   ` Efraim Flashner
2016-05-25  9:26 ` Ricardo Wurmus
2016-05-25 10:05   ` Efraim Flashner [this message]
2016-05-26  8:46 ` Ludovic Courtès
2016-05-26 17:50   ` Efraim Flashner
2016-05-26 19:27 ` Efraim Flashner
2016-05-27 13:37   ` Eric Bavier
2016-05-27 15:28     ` Taylan Ulrich Bayırlı/Kammer
2016-05-27 15:32       ` Thompson, David
2016-06-05 12:40 ` Efraim Flashner
2016-06-05 20:37   ` Ludovic Courtès
2016-06-07  7:41     ` Efraim Flashner
2016-06-08 15:43       ` Ludovic Courtès
2016-06-14  9:27         ` Efraim Flashner
2016-06-14  9:57           ` Ricardo Wurmus
2016-06-14 10:20             ` Efraim Flashner
2016-06-14 10:50               ` Efraim Flashner
2016-06-14 11:08                 ` Ricardo Wurmus
2016-06-15 13:56                 ` Ludovic Courtès
2016-06-15 20:28                   ` Efraim Flashner
2016-06-23  8:34                     ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160525100503.GB15009@debian-netbook \
    --to=efraim@flashner.co.il \
    --cc=guix-devel@gnu.org \
    --cc=ricardo.wurmus@mdc-berlin.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).