Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #76: Unoptimized hard drive


comp / comp.lang.lisp / String processing

SubjectAuthor
* String processingB. Pym
`- Re: String processingB. Pym

1
Subject: String processing
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Wed, 18 Sep 2024 23:13 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: String processing
Date: Wed, 18 Sep 2024 23:13:11 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <vcfmq5$8a1j$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 19 Sep 2024 01:13:11 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ed0184a7c4ccdaeb1e6757f422447af5";
logging-data="272435"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/AHC5E/PM0DpUr19oQAz/l"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:YlS2KNLa+ZhUriBnC5OyMZXS0UM=
View all headers

Pierre Mai wrote:

> > We've had this discussion before. Perl was designed to make file scanning
> > and pattern matching easy. The programs we've been designing in this
> > thread are precisely the kind of application that Perl is intended for.
> > I'm well-enough versed in Lisp to know that the equivalent of:
> >
> > while (<>) { # Loop over input lines
> > counter++ if /^\s*\(/; # if first non-white character is open-paren, count i
> t
> > }
> >
> > would be much more verbose without being significantly more expressive.
> -------------
>
> If it weren't for the regexp, which needs a comment of 10 words to
> explain what it does, and which is easy to get wrong (either comment,
> or regexp that is), I could believe that statement, but so I have to
> humbly disagree ;)
>
> Anyways, this is Common Lisp:
>
> (loop for line = (read-line *standard-input* nil nil)
> while line
> count (starts-with (left-trim-whitespace line) "("))
>
> This uses two trivial string functions which are probably part of
> every working CL user[1]. With an extensible LOOP facility, this could
> even be clarified further...
.....
> Footnotes:
> [1] Here are some very simple, inefficient sample implementations:
> (defun starts-with (string substring)
> "Detect whether the `string' starts with `substring'."
> (eql 0 (search substring string)))
>
> (defun left-trim-whitespace (string &optional (ws-bag '(#\Space #\Tab)))
> "Trims any whitespace characters (i.e. characters in `ws-bag') from
> the left side of `string'."
> (string-left-trim ws-bag string))

Gauche Scheme

(use gauche.generator)

Using a regular expression:

(generator-count #/^\s*[(]/ read-line)

Without using a regular expression:

(use srfi-13) ;; string-trim string-prefix?

(generator-count (^s (string-prefix? "(" (string-trim s))) read-line)

Subject: Re: String processing
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Thu, 19 Sep 2024 06:23 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: String processing
Date: Thu, 19 Sep 2024 06:23:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <vcgg1s$fg77$1@dont-email.me>
References: <vcfmq5$8a1j$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 19 Sep 2024 08:23:59 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="25924006d51a03bad00818fb787899cc";
logging-data="508135"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fBimhP7dqezhqL/Ye/7oI"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:7vMqUY6IueiqgSvSN60hviDjMMc=
View all headers

B. Pym wrote:

> Pierre Mai wrote:
>
> > > We've had this discussion before. Perl was designed to make file scanning
> > > and pattern matching easy. The programs we've been designing in this
> > > thread are precisely the kind of application that Perl is intended for.
> > > I'm well-enough versed in Lisp to know that the equivalent of:
> > >
> > > while (<>) { # Loop over input lines
> > > counter++ if /^\s*\(/; # if first non-white character is open-paren, count i
> > t
> > > }
> > >
> > > would be much more verbose without being significantly more expressive.
> > -------------
> >
> > If it weren't for the regexp, which needs a comment of 10 words to
> > explain what it does, and which is easy to get wrong (either comment,
> > or regexp that is), I could believe that statement, but so I have to
> > humbly disagree ;)
> >
> > Anyways, this is Common Lisp:
> >
> > (loop for line = (read-line *standard-input* nil nil)
> > while line
> > count (starts-with (left-trim-whitespace line) "("))
> >
> > This uses two trivial string functions which are probably part of
> > every working CL user[1]. With an extensible LOOP facility, this could
> > even be clarified further...
> ....
> > Footnotes:
> > [1] Here are some very simple, inefficient sample implementations:
> > (defun starts-with (string substring)
> > "Detect whether the `string' starts with `substring'."
> > (eql 0 (search substring string)))
> >
> > (defun left-trim-whitespace (string &optional (ws-bag '(#\Space #\Tab)))
> > "Trims any whitespace characters (i.e. characters in `ws-bag') from
> > the left side of `string'."
> > (string-left-trim ws-bag string))
>
> Gauche Scheme
>
> (use gauche.generator)
>
> Using a regular expression:
>
> (generator-count #/^\s*[(]/ read-line)
>
> Without using a regular expression:
>
> (use srfi-13) ;; string-trim string-prefix?
>
> (generator-count (^s (string-prefix? "(" (string-trim s))) read-line)

Another way.

(generator-count (=>> string-trim (string-prefix? "(")) read-line)

Given:

(define-syntax ->>
(syntax-rules ()
[(_ x) x]
[(_ x (y ...) z ...)
(->> (y ... x) z ...)]
[(_ x y z ...)
(->> (y x) z ...)]))

;; currying
(define-syntax =>>
(syntax-rules ()
[(_ func ...)
(lambda (x) (->> x func ...))]))

1

rocksolid light 0.9.8
clearnet tor